C ++ Memory Management 2 (memory allocation)

Source: Internet
Author: User

1. memory usage of C ++ compiled programs

(1) STACK: it is automatically allocated by the compiler when the program is running, and stores the function parameter values and local variable values. The operation method is similar to the stack in the data structure. The compiler Automatically releases the program when it ends.
(2) Heap: Another storage area is opened in the memory. Generally, it is assigned to the programmer for release. If the programmer does not release the program, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
(3) Global static zone (static): memory is allocated when the compiler compiles. Global variables and static variables are stored in one area, and initialized global variables and static variables are stored 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.
(5) code area: stores the binary code of the function body (class member functions and global functions.

2. There are three memory allocation methods
(1) distribution from the static storage area: the internal program has been allocated when it is compiled, and the internal program exists throughout the runtime. For example, global variables and static variables.
(2) create on Stack: During function execution, the storage units of local variables in the function can be created on the stack. When function execution ends, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
(3) allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.

3. c ++ variable Memory Distribution

Program description

(1) variable distribution

View code

# Include <stdio. h>
# Include <malloc. h>
Int g_ I = 100;
Int g_j;
Int main ()
{
Const int maxn = 100;
Int * P = (int *) malloc (maxn * sizeof (INT ));
Static int s_ I = 5;
Static int s_j;
Int I = 5;
Int J;
Char K = 'a ';
Char * pstr1 = "morewindows123456789 ";
Char * pstr2 = "morewindows123456789 ";
Char * pstr3 = "hello ";

Printf ("heap data address: 0x % 08x \ n", P );

Putchar ('\ n ');
Printf ("data address in stack (with initial values): 0x % 08x = % d \ n", & I, I );
Printf ("data address in stack (no initial value): 0x % 08x = % d \ n", & J, J );
Printf ("data address in stack (with initial values): 0x % 08x = % C \ n", & K, k );

Putchar ('\ n ');
Printf ("static data address (with initial values): 0x % 08x = % d \ n", & s_ I, s_ I );
Printf ("static data address (no initial value): 0x % 08x = % d \ n", & s_j, s_j );

Putchar ('\ n ');
Printf ("global data address (with initial values): 0x % 08x = % d \ n", & g_ I, g_ I );
Printf ("global data address (no initial value): 0x % 08x = % d \ n", & g_j, g_j );

Putchar ('\ n ');
Printf ("String constant data address: 0x % 08x points to 0x % 08x content is-% s \ n", & pstr1, pstr1, pstr1 );
Printf ("String constant data address: 0x % 08x points to 0x % 08x content:-% s \ n", & pstr2, pstr2, pstr2 );
Printf ("String constant data address: 0x % 08x points to 0x % 08x content:-% s \ n", & pstr3, pstr3, pstr3 );
Printf ("const constant data address: 0x % 08x = % d \ n", & maxn, maxn );
Free (P );
Return 0;
}

Running result:

View code

Heap data address: 0x003820d0

Stack data address (with initial values): 0x0012ff74 = 5
Stack data address (no initial value): 0x0012ff70 =-858993460
Stack data address (with initial values): 0x0012ff6c =

Static data address (with initial values): 0x0020.a34 = 5
Static data address (no initial value): 0x00427c48 =

Global data address (with initial values): 0x0020.a30 = 100
Global data address (no initial value): 0x00427c44 = 0

String constant data address: 0x0012ff68 points to 0x004221d8 and the content is-morewindows123456789
String constant data address: 0x0012ff64 points to 0x004221d8 and the content is-morewindows123456789
String constant data address: 0x0012ff60 points to 0x004221d0 content is-Hello
Const constant data address: 0x0012ff7c = 100
Press any key to continue





 

Analysis result (Debug ):

(1) The distribution of variables in the memory address is: Stack-heap-code zone-Global static-text constant Zone

(2) variables in the same region are allocated from low to high in the memory in the declared order, but the stack is from high to low.

(3) If global variables and static variables are not assigned a value, the default value is 0. If no value is assigned to the variable in the stack, It is a random data.

(4) The Compiler considers that global variables and static variables are equivalent. initialized global variables and static variables are allocated together, And uninitialized global variables and static variables are allocated together.

(2) Function Distribution

View code

# Include <stdio. h>
Void fun (int I)
{
Int J = I;
Static int s_ I = 100;
Static int s_j;

Printf ("subfunction parameters: 0x % P = % d \ n", & I, I );
Printf ("data address in the sub-function Stack: 0x % P = % d \ n", & J, J );
Printf ("subfunction static data address (with initial values): 0x % P = % d \ n", & s_ I, s_ I );
Printf ("subfunction static data address (no initial value): 0x % P = % d \ n", & s_j, s_j );
}
Int main ()
{
Int I = 5;
Static int s_ I = 100;
Static int s_j;

Printf ("data address in the main function Stack: 0x % P = % d \ n", & I, I );
Printf ("Main Function static data address (with initial values): 0x % P = % d \ n", & s_ I, s_ I );
Printf ("subfunction static data address (no initial value): 0x % P = % d \ n", & s_j, s_j );
Putchar ('\ n ');

Fun (I );
Return 0;
}

Running result

View code

Data address in the main function Stack: 0x0012ff7c = 5
Static data address of the main function (with initial values): 0x0020.a34 = 100
Subfunction static data address (no initial value): 0x00427c48 = 0

Sub-function parameter: 0x0012ff28 = 5
Data address in the sub-function Stack: 0x0012ff1c = 5
Static data address of the sub-function (with initial values): 0x0020.a30 = 100
Subfunction static data address (no initial value): 0x00427c44 = 0
Press any key to continue

Result Analysis:

The stack address in the main function is higher than the parameter and stack address in the subfunction, which proves that the stack stretching direction is extended from the high address to the low address. The addresses of static data in the primary and subfunctions are also adjacent, indicating that the program will allocate initialized global variables and declarative variables together, uninitialized global variables and declarative variables are assigned together.

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.