Memory allocation when compiling programs in C + +

Source: Internet
Author: User

The memory used by a program compiled by C + + is divided into the following sections

1. Stack (stack)-the program is automatically assigned by the compiler when it is run, storing the parameter value of the function, the value of the local variable, etc. It operates in a manner similar to a stack in a data structure. Automatically released by the compiler at the end of the program.

2. Heap area-open another storage area in memory. Usually released by the programmer, if the programmer does not release, the program ends may be recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.

3. Global zone (static zone)-the compiler allocates memory at compile time. 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 contiguous area. -released by the system after the program is finished

4, literal constant area-the constant string is put here. Released by the system after the program is finished

5. Program code area-binary code that holds the function body.

Example Program

It was written by a predecessor, 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/0" in the constant area, p3 on the stack.

static int c = 0;//global (Static) initialization area

P1 = (char *) malloc (10);

P2 = (char *) malloc (20);

Areas that are allocated 10 and 20 bytes are in the heap area.

strcpy (P1, "123456"); 123456/0 in the constant area, the compiler might optimize it to a place with the "123456" that P3 points to.

}

Reference: http://blog.csdn.net/gukesdo/article/details/6548256

Memory allocation when compiling programs in C + +

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.