The usual impression of the heap and stack is two kinds of data structure, the stack is advanced, the heap is FIFO. Here's a common example of the analysis:
Main.cpp
int a = 0; Global initialization Zone
Char *p1; Global uninitialized Zone
Main ()
{
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.
static int c = 0; global (static) initialization zone
P1 = (char *) malloc (10); Heap
P2 = (char *) malloc (20); Heap
}
I do not know if you have a little understanding, the first difference between heap and stack is the application method is different: Stack (English name is stack) is the system automatically allocated space, for example, we define a char A; the system automatically opens up space on the stack. The heap (the English name is heap) is the space that the programmer applies for, such as malloc (10), and 10 bytes of space to open. Since the space on the stack is automatically collected automatically, the life cycle of the data on the stack is only run in the function, it is released after running, and can no longer be accessed. The data on the heap can be accessed as long as the programmer does not free up space, but the drawback is that once you forget to release it will cause a memory leak.
Data types are classified as value types and reference types, and the base data type is the value type, stored in the stack memory, and the reference type is stored in heap memory.
Summarize:
(1) In JS, the array type is the reference type, so it is stored in the heap, that is, the address of the array is stored in the heap, and the value of the array is stored in the stack. General data such as Var a=10; a new space is created in the stack to hold A; var a=[1,2,3,4]; At this point the address of the array A is stored in the heap, and the address points to the value in the stack.
JS file:
Function ABC (a) {
a=100;
}
function ABC2 (arr) {
arr[0]=0;
}
/************************************************************************************************************** ******/
/*//value passing and reference passing
1. Value passing
var b=0.01;
ABC (b);
alert (b);
Output: 0.01
An array is a reference type and is passed an address
var arr1=[123,2343,435];
ABC2 (ARR1);
for (Var i=0;i<arr1.length;i++) {
Document.writeln (arr1[i]+ "");
}
Output: 0 2323 435
(2) Pay attention to the difference between value delivery and address delivery
The understanding of heap and stack in JS function abc (a) {a=100;} function ABC2 (arr) {arr[0]=0;}