Stack, heap, code area, global (static) area, Constant Area

Source: Internet
Author: User

It is generally considered that C is divided into these storage areas
1 stack-automatically assigned and released by a compiler
2 heap-it is generally assigned and released by the programmer. If the programmer does not release it, the program may be recycled by the OS at the end of the program.
3. In the global zone (static zone), the storage of global variables and static variables is put together. initialized global variables and static variables
State variables are in one region, and uninitialized global variables and uninitialized static variables are in another adjacent region.

-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 defined on the stack, and the memory is allocated using the malloc, calloc, realloc, and other functions.
The configuration is on the stack. In vitro, all functions define a global volume.
Are stored in the global zone (static zone). Static variables defined in all functions in vitro are valid in this file,
It cannot be used in other files. The static expression defined in the function body is valid only in the function body. In addition,
Strings such as "adgfdf" in the function are stored in the constant area.
For example:
Int A = 0; global initialization Zone
Char * P1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "ABC"; stack
Char * P2; stack
Char * P3 = "123456"; 123456 \ 0 is in the constant zone, and P3 is in the stack.
Static int C = 0; Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
The allocated 10-byte and 20-byte areas are in the heap area.

Strcpy (P1, "123456"); 123456 \ 0 in the constant area, the compiler may point it to "12345"
6 "optimized to one piece.
}
In addition, during function calling, a series of operations will be performed on the stack to retain the site and transmit parameters.
The stack space is limited. The default value of VC is 2 MB.. When the stack is insufficient, a large number of arrays and
The hierarchy of recursive functions is too deep. One thing you must know is that when a function is returned after calling it, it will release all
Stack space. The stack is automatically managed by the compiler, so you don't have to worry about it.
Heap dynamically allocates memory, and you can allocate a large amount of memory. However, poor use may cause memory leakage.
In addition, frequent malloc and free will generate memory fragments (a bit similar to disk fragments ),Because c allocates dynamic memory
Is to find matching memory. Stack does not produce fragments.

Accessing data on a stack is faster than accessing data on a stack through a pointer.
Generally, stack is the same as stack, that is, stack, and heap.
The stack is first imported and then output, and is generally grown from a high address to a low address.

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.