Discussion: The allocation of programs in-store (constants, local variables, global variables, program code) _c language

Source: Internet
Author: User
Tags constant valid

A. Divided into these storage areas in C
1. Stack-Automatically allocate release by compiler
2. Heap-typically released by programmers, if the programmer does not release, the program may end up being recycled by the OS
3. Global zone (static area), global variables and static variables are stored in a piece of the initialization of global variables and static variables in a region, uninitialized global variables and uninitialized static variables in another adjacent area. -Program End Release
4. There is also a special place for constants. -Program End Release

Variables defined in the function body are usually on the stack, with malloc, Calloc, realloc, and other functions allocated to the memory are allocated on the heap. The global quantity is defined in all functions, and the static modifier is added to the global area (static region), and the static variable defined in the body of all functions is valid in the file and cannot be used for other files. A static representation defined in a function body is only valid in the body of the function. In addition, a string such as "ADGFDF" in a function is stored in a constant area. Like what:

Copy Code code as follows:

Main.cpp
int a = 0; Global initialization Area
Char *p1; Global uninitialized Zone
void Main ()
{
int b; Stack area
Char s[] = "ABC"; Stack area
Char *p2; Stack area
Char *p3 = "123456";   P3 in the stack area; "123456\0" is in the constant area,

static int c = 0; Global (static) initialization area
P1 = (char *) malloc (10);
P2 = (char *) malloc (20); The 10 and 20 bytes allocated are in the heap area.
strcpy (P1, "123456"); "123456\0" is placed in a constant area, and the compiler might optimize it to a place with the "123456" that P3 points to.
}

Two. In C + +, the memory is divided into 5 areas, they are the heap, stack, free storage area, global/static storage and constant storage area
1. Stacks are those that are allocated by the compiler at the time of need, and are automatically aware of variables when they are not needed. The variables inside are usually local variables, function parameters, and so on.
2. Heap, that is, those allocated by the new memory block, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer is not released, the operating system will automatically recycle after the program finishes.
3. Free storage, which is the memory block allocated by malloc, he and the heap are very similar, but it is to use free to end their lives.
4. Global/static storage, global variables and static variables are assigned to the same memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + without this distinction, they share the same memory area.
5. Constant storage, this is a special storage area, they hold a constant, not allowed to modify (of course, you have to pass improper means can also modify)

Theory knowledge of heap and stack
How to apply
Stack:
automatically allocated by the system.   For example, declare a local variable int b in a function; The system automatically opens up space for B in the stack
heap: require the programmer to apply, and indicate the size, in C malloc function
such as P1 = (char *) malloc (10);
Using the new operator in C + +
such as P2 = (char *) malloc (10);
But note that P1, p2 itself is 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.