preface

The children who have learned the computer all know stack and heap, to tell the truth, when I really didn’t listen to the university, also know about, today is a simple summary, talking about stack and heap, to understand how we usually define variables, in the computer is stored.

What are stacks and stacks?

Heap and stack are actually two types of data structures. A stack is a data structure in which items are arranged in order, and items can only be inserted and deleted at one end (called the top of the stack). A stack is a special storage area whose main function is to temporarily store data and addresses.

What’s the difference between a heap and a stack?

  • Stack (operating system) : automatically allocated by the operating system to hold function parameter values, local variable values, etc. It operates like a stack in a data structure. When a variable is defined, the computer creates a space in memory to store the value of the variable. This space is called the stack. However, the stack usually holds the basic type of data.
  • Heap (operating system) : The heap is allocated and released by the programmer. If the programmer does not release the heap, the program may be recycled by the OS at the end of the program. Allocation is similar to a linked list. In fact, the general store of variables in the heap is some object types

To understand stacks and heaps in code?

So without further ado, let’s start with a simple piece of code and think about what it prints and why it prints. How is it represented in the heap and stack? Look at the corresponding explanation below, so that it may be better understood.

        //1
	var a = 10;
	b = a;
	b += 20;
	alert(a);
	
	//2
	var obj = {
		name : "jack"
	};
	b = obj;
	b.name = "Lily"; alert( obj.name ); Var arr = [1,2,3]; brr = arr; brr[0] = 10; alert( arr[0] ); //4 var a = 20;functionfun(a){ a += 30; } fun( a ); alert( a ); Var arr = [1,2,3];function fun(brr){
		brr[0] = 20;
	}
	fun(arr); 
	alert(arr[0]);
Copy the code

Above:



On this picture you can understand the first titled what prints 10, when you declare a variable, computer will give you open up in the stack a memory space to store the variable, when you change to b, it is only to change the b, so that a value is not affected by any of the space, so the first question will print out 10.

Continue with the image above:





What about arrays, objects, and so on? You can see that in the diagram above, corresponding to problem number three, if you define an arr = [1,2,3]. You’re going to store an address in the stack, you’re going to store the value of that address in the heap, and that address is going to point to the corresponding data, so what’s the name of that address in the computer? It’s called a pointer variable, BRR = arr; This is called address passing, and you can see that BRR points to the same location in the heap, so when you change BRR [0], arR [0] also changes, so the answer to the third problem is 10 instead of 1. Therefore, this kind of address transmission value is also called a complete change.

supplement

I don’t know what you have in mind when you are actually developing, that is the problem of deep copy and shallow copy. This problem is often encountered in development. For copy you can read my article, <– click here.

conclusion

The above explanation is easy to understand, there is not too much gorgeous language, look comfortable to understand is the most important, if you are to do the front end, if you are not a computer professional half monk I believe is also more help to you, of course, there are more professional explanation. You can think about the remaining questions by yourself. If it can help you better understand heap and stack, please give me a small praise as an encouragement, so that I will work harder to write the article.