Memory allocation mode of C + + programs

Source: Internet
Author: User
Tags constant

1. Memory allocation mode

There are three ways to allocate memory:

[1] is assigned from a static storage area. The memory is allocated when the program is compiled, and exists throughout the running of the program. such as global variables, static variables.

[2] created on the stack. When the function is executed, the storage units of local variables within the function can be created on the stack, and the storage units are automatically freed when the function is finished. Stack memory allocation operations are placed within the processor's instruction set, which is highly efficient, but allocates a limited amount of memory.

[3] Allocated from the heap, also known as dynamic memory allocation. The program uses malloc or new to request any amount of memory at run time, and the programmer is responsible for releasing the memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but if space is allocated on the heap, it is the responsibility to recycle it, otherwise the running program will have a memory leak, frequently allocating and releasing different sizes of heap space will generate heap fragments.

2. The memory space of the program

A program divides the memory blocks that the operating system assigns to run into 4 areas, as shown in the following illustration.

Code Area Program Memory Space
Global Data area
Heap Zone (heap area)
Stack region (Stack area)

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

1, stack area (stack)-Automatically assigned by the compiler release, stored for the run function and the allocation of local variables, function parameters, return data, return address, and so on. The operation is similar to the stack in the data structure.

2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the program at the end may be reclaimed by the OS. The allocation method is similar to a linked list.

3, the Global zone (static area) (static)-holds global variables, static data, constants. The system is released after the program is finished.

4, literal constant area-the constant string is here. The system is released after the program is finished.

5, program code area-Store function body (class member function and global function) binary code.

Here is an example program,

int a = 0; //全局初始化区
char *p1; //全局未初始化区
int main() {
int b; //栈
char s[] = "abc"; //栈
char *p2; //栈
char *p3 = "123456"; //123456在常量区,p3在栈上。
static int c =0;//全局(静态)初始化区
p1 = new char[10];
p2 = new char[20];
//分配得来得和字节的区域就在堆区。
strcpy(p1, "123456"); //123456放在常量区,编译器可能会将它与p3所指向的 "123456"优化成一个地方。
}

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.