Discussion: Program allocation in memory (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 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.
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:

Copy codeThe Code is as follows: // main. cpp
Int a = 0; // global initialization Zone
Char * p1; // not initialized globally
Void main ()
{
Int B; // stack Area
Char s [] = "abc"; // stack Area
Char * p2; // stack Zone
Char * p3 = "123456"; // p3 is in the stack zone; "123456 \ 0" is in the constant zone,

Static int c = 0; // global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20); // The allocated 10 and 20 bytes are in the heap zone.
Strcpy (p1, "123456"); // "123456 \ 0" is placed in the constant area, and the compiler may optimize it into one place as opposed to "123456" pointed to by p3.
}

2. In C ++, the memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.
1. Stack is the storage area for variables allocated by the compiler when needed and automatically identified when not needed. The variables are usually local variables and function parameters.
2. Heap refers to the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new compiler 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. The free storage zone 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. in the global/static storage area, global variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized ones, 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. (Of course, you can modify them by improper means)

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:The programmer needs to apply 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.