The difference between a dynamic variable and a static variable:
1. Storage location
Dynamic variables: stored in memory out-of-stack data area
Static variables: stored in the global data area (static data area)
2. Life Time
Dynamic variables: Depending on the location you define, such as what you define in a function, variables that are outside the scope of the function are invalidated
Static variable: Released at the end of the program
3. Scope
Dynamic variables: The same as you define the location to determine, and the 2nd is the same
Static variable: Valid in current file
The difference between heap and stack:
Heap Stack (stack)
1, memory allocation aspects:
Heap: Typically released by programmers, if the programmer does not release, the program may end up being recycled by the OS. Note that it is different from the heap in the data structure, and the distribution is similar to a linked list. The following keywords may be used: New, malloc, delete, free, and so on.
Stack: The compiler (Compiler) automatically allocates the release, storing the function's parameter value, the value of the local variable, and so on. It operates in a manner similar to a stack in a data structure.
2. Application Method:
Heap: Requires programmers to apply themselves and indicate size. In C, the malloc function such as P1 = (char *) malloc (10); Use the new operator in C + +, but note that P1, p2 itself is in the stack. Because they can still be considered as local variables.
Stack: Automatically assigned by the system. For example, declare a local variable int b in the function, and the system automatically opens up space for B in the stack.
3. System response:
Heap: The operating system has a linked list that records the free memory address, when the system receives the application, it iterates through the list, finds the first heap node that is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program, in addition, for most systems, The size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code can properly free up the memory space. In addition, because the size of the found heap node does not necessarily equal the size of the application, the system will automatically re-put the extra portion into the idle list.
Stack: As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
4. Size limitation:
Heap: Is the data structure that extends to the high address, which is a discontinuous memory area. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack address and the maximum capacity of the stack is the system pre-defined, under Windows, the size of the stack is fixed (is a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
5, Efficiency aspects:
Heap: Is the memory allocated by new, generally slow, and easy to produce memory fragmentation, but the most convenient to use, in addition, in Windows, the best way is to use VirtualAlloc to allocate memory, he is not in the heap, nor is the stack is directly in the process of the address space to retain a fast memory, Although it is most inconvenient to use. But the speed is fast, also the most flexible.
Stack: Automatically allocated by the system, faster. But programmers can't control it.
6, storage content aspects:
Heap: The size of a heap is typically stored in a heap at the head of a pile. The concrete contents of the heap are arranged by programmers.
Stack: The first stack in a function call is the address of the next instruction in the main function (the next executable statement of the function call statement) and then the parameters of the function, in most C compilers, the arguments are left-to-right and then local variables in the function. Note: Static variables are not in the stack. When the function call is finished, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.
7, Access efficiency aspects:
Heap: Char *s1 = "Hellow Word"; it is determined at compile time;
Stack: char s1[] = "Hellow Word"; it is assigned at run time; it is faster to use an array than a pointer, because the pointer needs to be brokered in the underlying assembly with the EDX register, and the array is read directly on the stack.
The difference between a "PHP" static variable and a dynamic variable