memory allocation of the program
In a computer system, the data that runs the application is stored in memory, different types of data, and the memory areas that are saved are different, including:
1. Stack area [stack]: Automatically allocated by the compiler release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure.
2, heap area [heap]: Generally by the programmer allocation release, if the programmer does not release, the program ends may be recycled by the OS. Note that it is different from the heap in the data structure, and is distributed in a way similar to a linked list.
3. Global zone [Static zone]: The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. Released by the system after the program is finished.
4, text constant area: store constant character wear. Released by the system after the program is finished.
5, program code area: the binary code that holds the function body.
6, register area: used to save the stack top pointer and instruction pointer (assembly operation)
Example:
1int a =0;//Global initialization Zone2Char *p1;//Global uninitialized Zone34int main (void) {5int e;//Stack area6Char d[] ="Kingdev";//Stack area7Char *p2;//Stack area8Char *p3 ="98765";//98765/0 in the constant area, p3 in the stack area9Staticint c =0; //10 strcpy ( P1, 98765"); // 98765/0 in the constant area, the compiler might optimize it with the "98765" that P3 points to as a place. 11 p1 = (char *) malloc ( Span style= "color: #800080;" >10); // The area allocated 10 bytes is in the heap 12 P2 = (char *) malloc (20) ; // The area allocated 20 bytes is in the heap 13 }
(i) data in the stack area
1, after the application starts, the operating system will open up memory space for the application in the stack area, for storing local variables and function parameters, etc.
2, iOS main path stack area size is 1m,mac main path stack area size of 8M;
3, the variables in the stack are assigned and released by the compiler;
4, the data in the stack in the form of "stack" management, LIFO (LIFO)
5. High efficiency of accessing variables in the stack area, no memory fragmentation
6, the variable name in the stack (without *) is equivalent to a pointer alias to the data in the stack, and the variable name can simplify the work of the program ape.
(ii) data in the heap area
1, because the space in the stack area is limited, iOS applications, objects are built in the heap;
2, the heap area includes system memory and virtual memory (hard disk cache), by all running applications shared use ;
3, the memory allocation in the heap area is the responsibility of the operating system, the operating system uses a linked list to maintain all the allocated memory records uniformly
4. Because the heap area is shared by all applications, the operating system records the allocated area of memory in a way that is anonymous (only the memory address and size are recorded, not the specific type)
5, in order to access the data in the heap area, it must be done by pointers , the type of pointer determines the way to access the data in the heap;
6, when a memory area is no longer used, the program needs to notify the operating system to reclaim the memory area, so that the memory area can be reused by other programs, otherwise, the region will never be reassigned again, this is "memory leak";
7, if an area has been released, still trying to access the zone, will prompt "Bad memory Access", which is "Wild pointer error"
8, compared to the stack area, the efficiency of the heap area is much lower, but also prone to memory fragmentation
9, compared to the stack area, the access mode in the heap is more flexible, the object occupies more memory can also be larger
(iii) response of the system after application
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.
Heap: First of all should know that the operating system has a record of the free memory address of the list, when the system receives the application of the program, it will traverse the list, look for the first space is larger than the requested space of the heap node, and then delete the node from the list of idle nodes, and the node's space allocated to the program, in addition, 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. Also, because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.
(iv) Size limits of applications
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 of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. 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.
(v) Comparison of application efficiency
The stack is automatically assigned by the system and is faster. But programmers can't control it.
Heap is the memory allocated by new, generally slower, and prone to memory fragmentation, But it is most convenient to use. In addition, under Windows, the best way is to allocate memory with VirtualAlloc, he is not in the heap, nor is the stack is directly in the process's address space to retain a piece of memory, although the most inconvenient to use. But the speed is fast, also the most flexible.
(vi) storage content in heaps and stacks
Stack: In a function call, the first stack 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 the local variables in the function. Note that 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.
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 the programmer.
(vii) Comparison of access efficiency
1s1[] = "aaaaaaaaaaaaaaa"; // AAAAAAAAAAA is assigned at run time; 2 char *s2 = "bbbbbbbbbbbbbbbbb"// BBBBBBBBBBB is determined at compile time.
However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap).
1 voidMain () {2 CharA =1;3 CharC[] ="1234567890";4 Char*p ="1234567890";5A = c[1];6A = p[1];7 return;8}
The corresponding assembly code:
Ten: a = c[1]; 004010678A 4D F1 mov cl,bytePTR [ebp-0Fh] 0040106A the4D FC movbytePTR [ebp-4],CL One: a = p[1]; 0040106D 8B -EC mov edx,dword ptr [ebp-14h]004010708A the onmov al,bytePTR [edx+1] 00401073 the $FC movbytePTR [ebp-4],al
The first reads the elements in the string directly into the register CL, while the second one reads the pointer values into edx and then reads the characters according to EdX, which is obviously slow.
respecting the author's labor achievements, reprint please specify: "Kingdev"
Storage of data in memory