Memory area division of C/C ++ programs

Source: Internet
Author: User

The memory occupied by a C/C ++ compiled program is divided into the following areas:

1. Stack)

It is automatically assigned and released by the system to store function parameter values and local variable values. It is a continuous storage area in the memory, extending from a low address to a high address.

2. Heap)

Distribution and release by programmers. If the programmer does not release the program, it may be recycled by the operating system at the end of the program. Its storage space is discontinuous in the memory, and the allocation method is similar to the linked list.

3. Static Zone)

It is also called the Global zone. After the program ends, it is released by the system to store global and static variables. The initialized global variables and static variables are in one area. uninitialized global variables and uninitialized static variables are stored in another adjacent area.

4. Text Constant Area

After the program ends, it is released by the system to store constants. string constants are placed here.

5. Code Area

Stores the binary code of the function body.

# Include <stdlib. h> # include <string. h> int A = 0; // global initialization zone. char * P1; // not initialized globally. int main (void) {int B; // stack area. char s [] = "ABC"; // stack zone. char * P2; // stack zone. char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack. static int C = 0; // static (global) initialization zone. // The allocated 10 and 20 bytes are in the heap area. P1 = (char *) malloc (10); P2 = (char *) malloc (20); // 123456 \ 0 is placed in the constant area. // The Compiler may optimize it to the "123456" pointed to by P3. strcpy (P1, "123456"); Return 0 ;}

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.