Partitioning of C + + memory storage areas

Source: Internet
Author: User

C + + Memory storage Area Division

Interview written Test frequently

In memory, the storage of data is not disorganized, but it has the corresponding partition, according to the data type to be placed in the corresponding location.

The storage area is from the highest storage address to the lowest storage address, in turn:

    • Command line parameter area: command line parameters and environment variables;
    • stack: refers to the compiler when needed, the storage area of the variable is automatically cleared when it is not needed. The variables inside are usually the function's parameter values, local variable equivalents, which operate in a way similar to stacks in data structures.
    • Heap area: Dynamically allocating memory. Usually released by the programmer, if the programmer does not release, at the end of the program, the operating system may be automatically recycled. It is not the same as the heap of data structures, and is distributed in a similar way to linked lists.
    • Data segment:
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 adjacent area, after the end of the program is released by the system. literal constant area: The constant string is put here. Released by the system after the program is finished.
    • Code snippet: Binary code that holds the body of the function
Main.cpp Program code Area 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\0 in constant area, p3 On the stack.     static int c = 0;//global (Static) initialization area     P1 = (char *) malloc ();     P2 = (char *) malloc (20); Areas that are allocated 10 and 20 bytes are in the heap area.     strcpy (P1, "123456"),//123456\0 is placed in a constant area, and the compiler may optimize it with the "123456" that P3 points to as a place. }

Reference Links:http://my.oschina.net/hnuweiwei/blog/224346 and the Java memory allocation is great! http://developer.51cto.com/art/201104/255220.htm Very detailed!

Partitioning of C + + memory storage areas

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.