Reference: " Advanced Programming for UNIX Environment" Chapter 7, section 7.6
C programs generally have the following parts of the composition
Body segment: Also called the text segment, which is the part of the machine instruction that has CPU execution. Normally, the body segment is shareable and read-only.
Initialize data segment: This segment is usually used as a data segment that contains variables that require explicit initial values in the program, such as declarations outside the function:int cnt = ten;
Non-initialized data segment: Typically this segment of data is called a BSS segment (block start symbol), and the kernel initializes the data in this segment before the program begins execution 0 or a null pointer. For example, out of function declaration:int arr[100];
Stacks: Automatic variables and the information required to save each function call are stored in this section. The return address of the calling function is also stored in the stack. The recursive function calls itself every time, using a new stack frame, so that a function
The variable set in use does not affect the variable of another function call function.
heap: Dynamic storage allocations are typically performed in the heap, and because of historical conventions, the heap is located between non-initialized data segments and stacks.
the size command reports the length of the body segment, data segment, and BSS segment ( units: bytes )
Test Example:
#include "apue.h" void Myexit (void) { printf ("I AM in Myexit.\n");} int main (void) { printf ("I AM in main.c\n"); return 0;}
Change the procedure to
#include "apue.h" void Myexit (void) { printf ("I AM in Myexit.\n");} int main (void) { printf ("I AM in main.c\n"); return 0;}
Conclusion: 1, uninitialized global variables are saved in BSS segment
When the program is changed to
#include "apue.h" void Myexit (void) { printf ("I AM in Myexit.\n");} int a = 1;int main (void) { printf ("I AM in main.c\n"); return 0;}
Conclusion:2. The initialized global variables are stored in the data segment.
When the program is changed to
#include "apue.h" void Myexit (void) { printf ("I AM in Myexit.\n");} static int a;int main (void) { printf ("I AM in main.c\n"); return 0;}
Conclusion:3. Uninitialized static variables are stored in BSS .
When the program is changed to
#include "apue.h" void Myexit (void) { printf ("I AM in Myexit.\n");} static int a = 1;int main (void) { printf ("I AM in main.c\n"); return 0;}
Conclusion:4. The initialized static variables are stored in the data section.
When the program is changed to
#include "apue.h" void Myexit (void) { int A; printf ("I AM in Myexit.\n");} int main (void) { printf ("I AM in main.c\n"); return 0;}
Conclusion:5. Local variables declared inside the function are stored in the stack segment
the contents of the uninitialized data segment are not stored in Program files on disk because the kernel is set to 0 before the program starts running. The segments that need to be stored in the program file are only body segments and initialization data segments.
The size command output in Linux does not include parts of the stack and heap, including only the text segment (test), the code snippet (data), and the Uninitialized Data segment (BSS) three parts.
Summary:
1.uninitialized global variable is saved in BSS Section
2. Theinitialized global variables are saved in the data segment
3. Uninitializedstatic variables are stored in the BSS Section
4. The initialized static variable is saved in the data section .
5.local variables declared inside the function are saved in the stack segment
6. Uninitializedstatic variables ( including global and layout variables ) are stored in the BSS Section
7, initialized static variables ( including global and local variables ) are saved in the data segment
C programs ' storage space