Program memory allocation (constant, local variable, global variable, program code)

Source: Internet
Author: User

I. In C, there are several storage areas
1. Stack-automatically allocated and released by the compiler
2. Heap-generally Program Members are assigned to release the program. If the programmer does not release the program, it may be recycled by the OS at the end of the program.
3. in the global zone (static zone), global variables and static variables are stored in one partition, and initialized global variables and static variables are stored in one partition, uninitialized global variables and uninitialized static variables are in another adjacent area. -Release after the program ends
4. There is also a special place to place constants. -Release after the program ends

The variables defined in the function body are usually on the stack. The memory allocated by using malloc, calloc, realloc, and other functions is on the stack. All functions define a global volume in vitro. After the static modifier is added, all functions are stored in the global zone (static zone) no matter where they are located ), static variables defined by all functions in vitro are valid in this file and cannot be used in other files. Static variables defined in the function body are valid only in this function. In addition, strings such as "adgfdf" in the function are stored in the constant area. For example:

 
// Main. cppint a = 0; // global initialization zone char * P1; // void main () {int B; // stack char s [] = "ABC"; // stack char * P2; // stack char * P3 = "123456"; // P3 is in the stack zone; "123456 \ 0" in the constant area, static int C = 0; // global (static) initialization area p1 = (char *) malloc (10); P2 = (char *) malloc (20); // The allocated 10 and 20 bytes are in the heap zone strcpy (P1, "123456"); // "123456 \ 0" in the constant zone, the compiler may optimize it to the "123456" that P3 points .}

2. In C ++, the memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.
1. Stack,
It is the storage area of variables that are automatically allocated by the compiler when necessary and clear when not needed. The variables are usually local variables and function parameters.
2. heap,It is the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new one corresponds to a Delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.
3. free storage zone,It is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.
4. Global/static storage ZoneGlobal variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized, in C ++, there is no such distinction. They share the same memory zone.
5. Constant storage area,This is a special storage area where constants are stored and cannot be modified (you can also modify them through improper means)

The above self-built http://www.cnblogs.com/JCSU/articles/1051579.html

3. Theoretical knowledge of heap and stack
Application Method
STACK: automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
Heap: requires the programmer to apply for and specify the size. In C, the malloc Function
For example, P1 = (char *) malloc (10 );
Use the new operator in C ++
For example, P2 = (char *) malloc (10 );

But note that P1 and P2 are in the stack.

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.