Distribution of memory in memory of different variables and functions in C + + programs

Source: Internet
Author: User

One C + + compiled programs consume memory in the following sections

1, the Stack area: The compiler automatically assigns the value of the parameters of the stored function, the value of local variables, and so on, the operation is similar to the stack in the data structure.

2, heap area: Generally by the programmer allocation release, if the programmer does not release, the program ends may have the system to recover. It is different from the heap in the data structure. The allocation method is similar to a linked list.

3. Global zone (Static zone): Global variables and static variables are stored in a block, initialized global variables and static variables in one area, uninitialized in another adjacent area.

Released by the system after the program is finished.

4, text constant area: the constant string is stored here. The system is automatically released after the program finishes.

5, program code area: the binary code that holds the function body.

Second, the theoretical knowledge of the stack

1. Application method

Stack: The system is automatically assigned. For example, define a local variable, int i = 0, and use a value to pass when the function is passed.

Heap: Requires the programmer to apply and specify the size. For example, use the malloc function and the new operator.

2. System response after application

Stack: As long as the remaining space of the stack is greater than the requested space, the system will provide memory for the program, responsible for reporting stack overflow exception.

Heap: This design to the system's memory management, the operating system has a record of the free memory address of the list, and then according to the system's memory allocation policy allocation memory.

3, the application size limit

Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This means that the address and maximum capacity of the bottom and top of the stack are

The system is pre-defined. Under Windows It is said that the stack size is 2M and will prompt overflow if the requested space exceeds the remaining space on the stack. So the Stack's

Small space.

Heap: A heap is a data structure that extends to a high address, and is a discontinuous memory area. Because the system uses a linked list to store the address of the free memory, the direction of the linked list traversal

is from the low address to the high address. The size of the heap is limited by the effective virtual memory in the computer system. The space obtained is more flexible and larger.

4, Efficiency aspects

Heap: Slow speed, easy to produce debris, but easy to use. The quickest thing in Windows is to use VirtualAlloc to allocate memory, he is not in the heap and not on the stack,

Instead, a chunk of memory is reserved directly in the address space of the stack. Fast and flexible to use.

Stack: fast, but automatically assigned and controlled by the system.

5, storage content aspects

Heap: The size of the heap is usually placed in the heap with a byte in the head. The concrete contents of the heap are arranged by programmers.

Stack: The first step in a function call is the address of the next instruction in the main function (the function executes the statement), and then the parameters of each function.

In most C + + + compilers, function arguments are in the stack from right to left and then local variables in the function. Note: Static variables are not as good as stacks. This time the function is adjusted

After the end, the local variable first out of the stack, and then the function parameters, the last stack pointer to the first stored in the main function of the next instruction address, the program from the point following

Continuous operation.

Iii. Examples of interpretation

Global initialization Zone
int i1 = 0;
int i2 = 0;
int i3 = 0;

Global initialization Zone
static int i4 = 0;
static int i5 = 0;
static int I6 = 0;

Global uninitialized Zone
int i7;
int i8;
int I9;


void Creat ()
{
cout<< "Creat" <<endl;
}

void Add ()
{
cout<< "Add" <<endl;
}

void Delete ()
{
cout<< "Delete" <<endl;
}

int Max (int a,int b)//When this function is called, the argument starts from right to left and pushes the stack
{
Return a>b?a:b;
}

int _tmain (int argc, _tchar* argv[])
{
cout<< "Print global initialization zone variable I1-I3 address:" <<endl;
cout<<&i1<< "" <<&i2<< "" <<&i3<<endl;

cout<< "Print global initialization zone static variable I4-i6 address:" <<endl;
cout<<&i4<< "" <<&i5<< "" <<&i6<<endl;

cout<< "Print global uninitialized Zone variable i7-i9 address:" <<endl;
cout<<&i7<< "" <<&i8<< "" <<&i9<<endl;

cout<< "Print the above three functions sequentially creat, add, delete address:" <<endl;
cout<<&creat<<endl;
cout<<&add<<endl;
cout<<&delete<<endl;

Stack area
int C1 = ' a ';
int C2 = ' B ';
int C3;
int C4;

cout<< "Print the local variable C1-C4 address within the main function, where c3,c4 is uninitialized" <<endl;
cout<<&c1<< "<<&c2<<" "<<&c3<<" "<<&c4<<" < <endl;

Char *pstr1 = "12345";//12345 in constant area, pstr on stack
Char *pstr2 = "1122";

void *p = PSTR1;
void *q = PSTR2;
cout<< "Print constant Address" <<endl;
cout<<p<<endl;
cout<<q<<endl;

static int i10 = 0;//Global (static) initialization area
cout<< "Define the static variable address in the local function, please compare the other global zone addresses promised above" <<endl;
cout<<&i10<<endl;

int *p1 = new int;//heap Area
int *p2 = new int;//heap Area
cout<< "Print heap area Address" <<endl;
cout<<p1<<endl;
cout<<p2<<endl;

strcpy (PSTR1, "1144");//12345 in a constant area, the compiler might optimize the PSTR1 literal constant 1144 into a single place

GetChar ();
return 0;
}

Output:

The above results are tested in VS2008. For the above results, it is observed that the variables of the global uninitialized zone are defined by the declaration from high to low address.

sequential stack, variable i7 the first variable i1 of the global initialization segment. The variables of the global initialization segment (including Static, do not differentiate) from the low address

To the high address in the declared order of the stack (not referring to the above mentioned stack area, please distinguish, this is the address change process, you will see it with

Local function variable start address is completely different). The address in the program code snippet is incremented by the declaration order function. The function local variables go in the stack according to

Declare the order from the high-to-low address into the stack. It is unclear whether the compiler causes the difference between the address of a few of the int variables that I have defined here, which is 12 bytes.

The starting address of a constant is the same as a global variable. The address of the heap area begins with another segment.

One thing to add: the inner elements of an array variable are stacked according to the element subscript from the low address to the high address.

General local variables are generally from high and low addresses to lower address stack.

From the above results, the global variables may actually be in the heap area.

The above content mainly refer to http://blog.csdn.net/benny5609/article/details/2217258 original article

Distribution of memory in memory of different variables and functions in C + + programs

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.