C ++ stack and stack

Source: Internet
Author: User
I. prerequisites- Program Memory Allocation the memory occupied by a C/C ++ compiled program is divided into the following parts 1. Stack-automatically allocated and released by the compiler, stores the function parameter values, the value of a local variable. The operation method is similar to the stack in the data structure. 2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure.
It is similar to a linked list. 3. Global (static)-stores global variables and static variables. initialized global variables and static variables are in the same region, but not initialized global variables.
Variables and uninitialized static variables are in another adjacent area. -Release the system after the program ends. 4. In the text constant area, the constant string is placed here. After the program ends, the system releases the program. Code Zone-stores the binary code of the function body.
Ii. Example program this is written by a senior, very detailed // 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 is in the constant zone, and P3 is in the stack. Static int C = 0; Global (static) initialization zone p1 = (char *) malloc (10); P2 = (char *) malloc (20 ); the allocated 10-byte and 20-byte areas are in the heap area. Strcpy (P1, "123456"); 123456 is placed in the constant area, and the compiler may optimize it to the "123456" that P3 points. }
Ii. Theoretical knowledge of heap and stack
2.1 application method Stack: automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens a space heap for B in the stack: the programmer needs to apply for it and specify the size, in C, malloc functions such as P1 = (char *) malloc (10); in C ++, use the new operator such as P2 = (char *) malloc (10 ); but note that P1 and P2 are in the stack.
2.2 System Response stack after application: as long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program. Otherwise, an exception will be reported, prompting stack overflow. Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application, it will traverse the linked list, find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and allocate the space of the node to the program.
Most systems will record the size of the allocation at the first address in the memory space so that the delete statement in the code can correctly release the memory space. In addition
The heap node size is not necessarily equal to the applied size. The system will automatically re-place the excess part into the idle linked list.
2.3 apply for a size limit Stack: in windows, the stack is a data structure extended to the low address and is a continuous memory area. This statement indicates that the top stack address and the maximum stack capacity are pre-defined by the system.
In Windows, the stack size is 2 m (OR 1 m, which is a constant determined during compilation). If the applied space exceeds the remaining space of the stack, overflow will be prompted
. Therefore, the space available from the stack is small. Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal of the linked list
The direction is from low address to high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
2.4 comparison of application efficiency: the stack is automatically allocated by the system, which is faster. But programmers cannot control it. The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
In addition, in windows, the best way is to use virtualalloc to allocate memory. It is not in the heap, nor in the stack, it is directly in the address space of the process to reserve a fast memory, although it is used up.
Most inconvenient. However, it is fast and flexible.
2.5 heap and the storage content stack in the stack: During function calling, the first entry to the stack is the address of the next instruction (the next executable statement of the function call statement) in the main function, then there are various parameters of the function, in most
In the number C compiler, the parameter is from right to left into the stack, and then is the local variable in the function. Note that static variables are not included in the stack. When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function. The program continues from this point.
Run. Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer. 2.6 comparison of access efficiency char S1 [] = "aaaaaaaaaaaaa"; char * S2 = "bbbbbbbbbbbbbbbbb"; aaaaaaaaaaaaa is assigned a value at the runtime, while bbbbbbbbbbb is determined at the Compilation Time; however, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap. For example: # include void main () {char a = 1; char C [] = "1234567890"; char * P = "1234567890"; A = C [1]; A = P [1]; return ;}
The corresponding assembly code 10: A = C [1]; 00401067 8A 4D F1 mov Cl, byte PTR [ebp-0Fh] 0040106a 88 4D FC mov byte PTR [ebp-4], CL 11: A = P [1]; 0040106d 8B 55 EC mov edX, dword ptr [ebp-14h] 00401070 8A 42 01 mov Al, byte PTR [edX + 1] 00401073 88 45 FC mov byte PTR [ebp-4], Al
The first type reads the elements in the string directly into the CL register, while the second type reads the pointer value into EDX. Reading the characters based on edX is obviously slow.
2.7 conclusion: the difference between stack and stack can be seen in the following metaphor: using Stack is like eating at a restaurant, just ordering food (issuing an Application) pay, and eat (use). If you are full, you don't have to worry about cooking, washing dishes, and other preparation work, washing dishes, and flushing pots.
The advantage of scanning tail is that it is fast, but has little freedom. Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom. The differences between stack and stack are mainly divided into the heap and stack of the operating system, as mentioned above. There is also the heap and stack in the data structure. These are different concepts. Here, the heap actually refers to a Data Structure of the priority queue (meeting the heap nature), with the 1st elements having the most
High priority; stack is actually a mathematical or data structure that satisfies the advanced nature. Although the stack is called a connection, they still have a lot of difference. The connection is only due to historical reasons.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.