C/C ++ memory Division

Source: Internet
Author: User

I. The C language is divided into several categories:

1. stack zone stack)

The compiler automatically allocates and releases functions, stores function values, and local variable values. The operation method is similar to the stack in the data structure.

2. heap)

Generally, it is assigned and released by the programmer. If the programmer does not release the program, it may be recycled by the operating system when the program ends. Note that it is the same as the data structure heap. The allocation method is similar to the linked list.

3. Global static zone static)

Global variables and static variables are stored together. All initialized variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system.

4. Text Constant Area

Constant strings are placed here. The program is released by the system after it is completed.

5. program code area

Stores the binary code of the function body.

# Define does not occupy memory units. The C ++ compiler usually saves the definition of the const variable in the symbol table instead of allocating storage space to const constants. In VC, the const variable is the same as the general variable, and the memory space is allocated.


The variables defined in the function body are usually on the stack. The memory allocated by using malloc, calloc, realloc, and other functions is on the stack.

All functions define a global volume in vitro. After the static modifier is added, all functions are stored in the Global static zone no matter where they are ), static variables defined by all functions in vitro are valid in this file and cannot be used in other files. static variables defined in the function body are valid only in this function.

In addition, strings such as "adgfdf" in the function are stored in the constant area.

For example, int a = 0; // global initialization Zone

Char * p1; // not initialized globally

Void main (){

Int B; // Stack

Char s [] = "abc"; // Stack

Char * p2; // Stack

Char * p3 = "123456"; // 123456 {post. content} is in the constant area, and p3 is on the stack.

Static int c = 0; // Global static) initialization Zone

P1 = (char *) malloc (10); // The allocated 10-byte area is located in the heap area.

P2 = (char *) malloc (20); // The allocated 20-byte area is located in the heap area.

Strcpy (p1, "123456"); // 123456 {post. content} is placed in the constant area, and the compiler may optimize it with "123456" pointed to by p3}


II. C ++ is divided into the following types: heap, stack, free storage, global/static storage, and constant storage.

1. Stack is the storage zone for variables allocated by the compiler when needed and automatically cleared when not needed. The variables are usually local variables and function parameters.

2. Heap refers to the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new compiler corresponds to a delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.

3. The free storage zone is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.

4. in the global/static storage area, global variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized ones, in C ++, there is no such distinction. They share the same memory zone.

5. Constant storage area, which is a special storage area. It stores constants and cannot be modified. Of course, you can modify them by improper means)


This article is from the "IT migrant workers a" blog, please be sure to keep this source http://7633061.blog.51cto.com/7623061/1266355

Related Article

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.