Memory usage of C/C ++ compiled programs

Source: Internet
Author: User
Compiled by C/C ++ Program The memory used is divided into the following parts:
1. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.
2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, 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)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system is released after the program ends.
4. Text Constant Area-constant strings are placed here. The program is released by the System
5. Program Code Zone-stores the binary code of the function body.
-

2. Example(I)

IntA = 0;//Global Zone

 

VoidMain ()

{

IntB;//Stack

CharS [] = "ABC ";// SStack, "ABC"In the text Constant Area

Char* P1, * P2;//Stack

Char* P3 = "123456 ";// "123456"In the constant area,P3On Stack

Static IntC = 0;//Global Zone

P1 = (Char*) Malloc (10 );// P1In the stack, the allocated10Byte in heap

P2 = (Char*) Malloc (20 );// P2In the stack, the allocated20Byte in heap

Strcpy (P1, "123456 ");// "123456"Put in the constant area

//The compiler mayP3Point"123456"Optimized to one place.

}

 

 

3. Example(II)

//ReturnCharType pointer

Char* F ()

{

// SThe array is stored on the stack.

CharS [4] = {'1', '2', '3', '0 '};

ReturnS;//ReturnSArray address, but the program is runningSThe array is released.

}

VoidMain ()

{

Char* S;

S = f ();

Printf ("% s", S );//Print out garbled characters. BecauseSThe specified address has no data.

}

 

--------------------------------------------------------------

> Char * P = "1234567890 ";
> Is there any problem with this statement?

This statement is problematic and is explained as follows:

"1234567890" is a String constant. Both the C and C ++ standards stipulate that the result of any attempt to change a String constant is "undefined ". However, the pointer definition of the preceding statement cannot prevent the behavior of changing the String constant "1234567890" through the pointer P, for example, * P = 'a';, which cannot be found by the compiler.

The correct definition method should be: const char * P = "1234567890 ";

> Can pointer be initialized like this ???
The answer is OK. For historical reasons, the standard has made some "compromise" on this point: it allows the automatic conversion of character constant pointers to non-constant pointers. However, the user should know that the above problems exist. For this reason, the C ++ standard marks this conversion as "deprecated" and should be avoided.

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.