C program storage layout

Source: Internet
Author: User

Memory occupied by a C/C ++ compiled program is divided into the following parts:
1. program code area (. text)-stores the binary code of the function body.
2. In the text Constant Area (. rodata), the constant string is placed here. After the program ends, it is released by the system (rodata-read only data ).
3. Global/static-the storage of global and static variables is one piece. The initialized global variables and static variables are in one area (. rwdata or. data), uninitialized global variables and uninitialized static variables are in another adjacent area (. bss), the program is released by the system after it is completed.
* In C ++, bss and data are no longer strictly distinguished. They share a memory area.
4. heap (heap)-generally assigned and released by programmers (new/malloc/calloc delete/free). If the programmer does not release the heap, it may be recycled by the OS at the end of the program.
Note: It is different from the heap in the data structure, but the allocation method is similar to the linked list.
5. stack: the stack is automatically allocated and released by the compiler, and stores the function parameter values and local variable values. The operation method is similar to the stack in the data structure.


The computer program memory is organized into the following:
Code segment (text segment)
Data Segment
-- Data (rodata + rwdata)
-- BSS
-- Heap
Stack Segment

Data
The data area contains global and staticvariables used by the program that are initialized. This segment can be furtherclassified into initialized read-only (rodata) area and initialized read-writearea (rwdata ).
BSS
The BSS segment also known as uninitialized datastarts at the end of the data segment and contains all uninitialized globalvariables and static variables that are initialized to zero by default.
Heap
The heap area begins at the end of the BSSsegment and grows to larger addresses from there. the heap area is managed bymalloc/calloc/realloc/new and free/delete, which may use the brk and sbrk system callto adjust its size. the heaparea is shared by all shared libraries and dynamically loaded modules in aprocess.
Stack
The stack is a LIFO structure, typically locatedin the higher parts of memory. it usually "grows down" with everyregister, immediate value or stack frame being added to it. A stack frameconsists at minimum of a return address
 
Example Program
[Cpp]
// Main. cpp
Int a = 0; // global initialization zone (data)
Char * p1; // globally uninitialized (bss)
Int main ()
{
Int B; // stack)
Char s [] = "abc"; // stack)
Char * p2; // stack)
Char * p3 = "123456"; // p3 in the stack area (stack); "123456 \ 0" in the constant area (rodata)
Static int c = 0; // global/static initialization area (data)
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20); // The allocated 10 and 20 bytes are in the heap area)
Strcpy (p1, "123456"); // put "123456 \ 0" in the constant area (rodata ). the compiler may optimize it to the "123456 \ 0" pointed to by p3.
Return 0;
}
Stack and stack differences
Management Method: the stack is automatically managed by the compiler. For the heap, the release is controlled by the programmer and memory leak is easily generated.
Space size: Generally, in a 32-bit system, the heap memory can reach nearly 4 GB. From this perspective, there is almost no limit on the heap memory. But for stacks, there is usually a certain amount of space. For example, under VC6, the default stack space is about 1 MB.
Fragmentation problem: for the heap, frequent new/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments, reducing program efficiency. For the stack, this problem will not exist, because the stack is an advanced post-release queue, it is never possible to have a memory block popped up from the middle of the stack.
Growth direction: For the stack, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.
Allocation Method: The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler. For example, the allocation of local variables and dynamic allocation are performed by the alloca function, but the dynamic allocation of stacks is different from that of stacks, its Dynamic Allocation is released by the compiler and does not need to be implemented manually.
Allocation Efficiency: the stack is the data structure provided by the machine system. The computer will allocate a special register to store the stack address at the underlying layer, and the output stack of the Pressure Stack has dedicated command execution, this determines that the stack efficiency is relatively high; the stack is provided by the C/C ++ function library, and its mechanism is very complicated, for example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the Program Data Segment and then return it. Obviously, the heap efficiency is much lower than the stack efficiency.
Both heap and stack should prevent the occurrence of out-of-bounds phenomena.

A discussion on Global and Static types
1. What is the difference between static global variables and common global variables?
The definition of global variables (external variables) is preceded by static to form a static global variable.
Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods.
The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program.
Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.
Static global variables are modified only once to prevent being referenced in other file units.
2. What is the difference between static local variables and common local variables?
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.
Static local variables are initialized only once, and the next time is based on the previous result value.
3. What is the difference between a static function and a common function?
Static functions have different scopes than normal functions. They are only available in this file. Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file. the static function has only one copy in the memory (. data), normal functions maintain a copy in each call.

Related Article

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.