The organization of the stack frame--c/c++ memory management must be mastered

Source: Internet
Author: User

Program Stack

When it comes to stack frames, you need to start by talking about the program stack.

The Memory function program stack area is a support operation that typically shares the heap.

The stack usually occupies the lower part of the memory area, while the heap uses the top.

Stack frames are stored in the stack frame, sometimes called active records or active frames. Stack frames hold function parameters and local variables. The heap stores dynamic memory.

When the function is called, the stack frame of the function is pushed onto the stack. The stack grows up a stack frame.

When the function terminates, its stack frame pops up from the program stack.

The memory used by the stack frame is not cleaned up, but it can finally be pushed onto the stack with a stack frame overlay.

Dynamically allocated memory comes from the heap. The heap grows downward. With the allocation and release of memory. The heap is covered with debris.

Although the heap is growing downward, this is only in the general direction, and the actual memory may be anywhere on the heap.

Organization of Stack Frames

The stack frame consists of several elements:

return Address: The internal address of the program to be returned when the function is complete local data store: Memory parameter allocation for local variables storage: memory stack pointers and base pointers allocated for function parameters: the pointer that the system uses to manage the stack at execution time

The stack pointer usually points to the top of the stack. The base pointer usually exists and points to the address inside the stack frame, such as the return address, which is used to coordinate access to the elements inside the stack frame.

Both pointers are not C pointers, they are the address of the hypervisor stack at execution time.

The following function gives an example:

float average (int *arr, int size) {     int sum;     printf ("Arr:%p\n", &arr);     printf ("Size:%p\n", &size);     priintf ("Sum:%p\n", &sum);     for (int i = 0;i < Size;i + +) {          sum +=arr[i];     }          return (sum *1.0f)/size;} output//arr:0x500//size:0x504//sum:0x480


An empty file between the parameter address and the local variable. Saves the other stack frame elements that are required to execute the System management stack.

When creating a stack frame, the system pushes the arguments to the stack in the reverse order of the Declaration, pushing the local variable. In this example, size is pushed in after arr. In general, the return address of a function call is then pushed. Then a local variable. The order in which they are pushed is reversed in the order in the code!

In principle, the stack in this example grows upward. Only the arguments and local variables of the stack frame and the new stack frame are added to the low memory address.

The actual growth direction of the stack is actually related.

I in the For statement is not included in the stack frame. This is due to the fact that the block statement is considered a "miniature" function in the C language.

When the stack frame is pushed onto the stack, the system may run out of memory, which is called a stack overflow, which generally causes the program to terminate abnormally. Keep in mind that each thread has its own stack, and that one or more threads accessing the same object in memory can cause conflicts.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

The organization of the stack frame--c/c++ memory management must be mastered

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.