Memory four-zone model and function call model of C language

Source: Internet
Author: User

First, the operating system loads the code program into memory.

Then divide the memory into 4 zones

The stack area, the local variable area of the program, the parameters passed by the function, are automatically freed by the compiler for memory resources.

Heap area, dynamic memory request, if you do not manually free memory, this block of memory will not be destructor.

Global zone, static zone, constant area (where the string is stored), and after the program is finished, there is an operating system release

Code area, which holds the binary code of the function body.

Finally, the operating system finds the entry to the main function and begins execution of the code.

The opening direction of the stack in the general memory quad is downward. Why design this, because the design stack in the direction of the downward, you can set the size of the application stack, so as to avoid stack overflow.

Whether the stack is open or open, the buff's growth direction is upward. It can be measured by code.

If the opening direction of the stack is downward, then the base address of the buff is below, if the opening direction of the stack upward, the base address of the buff is also below, that is, regardless of the stack opening direction toward there, the base of the buff is below,

The invocation model of the function:

When the operating system calls the main function, the return address and parameters of the main function are put into the stack, and then the main function is started, and if other functions are called in the main function, the running state of the main function is put into the stack, then the return value of the called function is put into the stack, and the parameters of the called function are stacked It then executes the called function, and if there are other calling functions, the process is similar.

The memory that is allocated in the main function can be used by the called function.

The main function can allocate memory on the stack, on the heap, on the global zone, and these memory can be used by functions called functions.

The memory allocated on the stack in the called function cannot be used by the main function, like the heap, or the memory allocated on the global zone, which can be used by the main function, passing the first address of the memory by indirect assignment.

These are fairly easy to understand content

Memory four-zone model and function call model of C language

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.