On js heap and stack

Let me talk about two concepts here: 1. Heap (heap) 2, the stack (stack)
heap  is a heap memory for short.
Stack  is the stack memory for short.
Speaking of the stack, we are talking about is memory usage and distribution, there is no register of thing, there is no hard thing.
Various languages on the principle of the process stack are similar. The heap is dynamically allocated memory, memory sizes, it will not automatically released. The stack is automatically assigned a relatively fixed amount of memory space, it is automatically released by the system.

Basic types javascript on five kinds: Undefined, Null, Boolean, Number and String, which are directly on the value stored in the stack, the size of each type of data memory space occupied are determined, automatically assigned by the system and automatically released. Such benefits is that memory can be recovered in time, relative to the stack, the more easily manage memory space.

javascript other types of data are referred to as reference types of data: The object (Object), the array (the Array), function (Function) ..., which are new and by copying out, such data is stored in the heap. In fact, say that is stored in the heap, it is not accurate, because, quote address pointer of the type of data is stored in the stack, when we want to access the value of a reference type, we need to get the object's start with the stack address pointer, then, to find the desired data in the stack by the address pointer.

To say is the image of the stack, linear structure, LIFO, easy to manage. Heap, a chaotic, disorganized, convenience store and opened up memory space
by value and pass-by

var arr1 = [1,2,5,8]; var arr2 = arr1 ; var str1 = arr1[2]; console.log(arr2);//1,2,5,8 console.log(str1);//5 arr2[4] = 99; str1 = 6; console.log(arr1);//1,2,5,8,99 console.log(arr1[2]);//5

The above example that, when I change the data in the arr2, arr1 data has changed, when you change the data value of str1, arr1 has not changed. why? This value is the difference between pass and pass sites.

Because arr1 is an array, are reference type, it gives time to pass arr2 stack is the address (corresponding to a different new name "pointer"), rather than the value of the object in the stack memory. str1 obtained is a basic type of the assignment, and therefore, only str1 value acquired from arr1 a heap memory, and directly stored in the stack. arr1, arr2 all point to the same piece of heap memory, arr2 modify the heap of time, that will affect the arr1, str1 is modified directly in the stack, and can not affect the data arr1 heap memory.

Shallow vs. deep copy

When it comes to the way top assignment is shallow copy, then what is called a deep copy of it? That is, each basic type of data you want to arr1 traverse again, turn the arr2 assigned to the corresponding field. To avoid problems caused because address references.

var arr1 = [1,2,5,8]; var arr2 = []; for(var i=0;i<arr1.length;i++){ arr2[i]=arr1[i]; }; console.log(arr2)//1,2,5,8 arr2[4]=99; console.log(arr2)//1,2,5,8,99 console.log(arr1)//1,2,5,8

javascript object oriented language itself on the object and non-object processing proceeds classified, from the point of view of data structure value, the object is a stack pointer and stack.

Guess you like

Origin www.cnblogs.com/allen-xing0910/p/10954231.html