C/C ++ variable Storage Area

Source: Internet
Author: User
The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. STACK: automatically allocated by the compiler when the program is running, storing the function parameter values and local variable values. The operation method is similar to the stack in the data structure. 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)-memory allocated upon compilation by the compiler. 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 after it ends. 4. The text Constant Area-constant string is placed here. The program is released by the system. 5. program code area-stores the binary code of the function body.

Note: static local variables and static global variablesThe amount of static storage is not necessarily a static variable. For example, a global variable is a static storage method, but not necessarily a static variable. It must be defined by the static variable before it can become a static external variable or a static global variable. After a local variable is changed to a static variable, its storage mode is changed, that is, its lifetime is changed.
After changing a global variable to a static variable, it changes its scope and limits its scope of use.

See the following example:

# Include <iostream> using namespace STD; char * func () {// char STR [] = "Hello, world \ n"; // error string array, local variable, stored in the stack; char * STR = "Hello, world \ n" can be modified; // The okay String constant is stored in the constant zone; return STR cannot be modified ;} int main () {cout <func (); Return 0 ;}

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.