The difference between global variables, static global variables, static local variables, local variables

Look at the code in memory allocated from the :(:  http://www.cppblog.com/prayer/archive/2009/08/17/93594.html )

The BSS: the BSS (bss segment) generally refers to an area of memory used to store program uninitialized global variables. BSS is the English abbreviation Block Started by Symbol. BSS segment belongs to the static memory allocation.

Data segments: the data segment (data segment) generally refers to an area of memory used to store program initialized global variables. Data segments belonging to static memory allocation.

Code segment: code segments (code segment / text segment) generally refers to an area of memory used to store program code executed. The size of this partial region just before the program run has been determined, and usually belong to a read-only memory area, some architectures also allow for the code segment can be written, which allows to modify the program. In the code segment also may contain a constant read-only variables, such as string constants and the like.

Heap (heap): heap is used to store the processes running in memory segments are dynamically allocated , its size is not fixed, dynamically expanding or shrinking. When a process calls malloc to allocate memory and other functions, the newly allocated memory was dynamically added to the heap (heap expansion); when using the free function to release the memory, the memory is released is rejected (the stack is reduced) from the heap

Stack (stack): Stack, also known as the stack, local variables to store user program temporarily created , the variable that is a function of our brackets "{}" as defined variable (but not including static declaration, static means that the data segment variable storage). In addition, when the function is called, its parameters will be pushed to initiate the process stack called, and until the end of the call, the return value will be stored back into the stack. Due to the characteristics of the FIFO stack, the stack is particularly convenient to save / restore call site. In this sense, we can stack as a deposit, exchange data temporary memory area

Then in terms of their differences from :(:  https://blog.csdn.net/Jacketinsysu/article/details/51171483 )

In the link very detailed, there is not further described herein.

 

Static global variables have the following characteristics:
the variable allocates memory in the global data area (data segment);
uninitialized static global variable is automatically initialized to 0 (the value of the variable is automatically declared vivo random function, unless it be explicitly initialized automatically variable in vitro function is declared will be initialized to 0);
static global variable declaration in its entire file is visible, in addition to the documents is not visible;
static variables are global data area allocated memory, including static local variables will be mentioned later. For a complete program, the distribution in the memory as shown below:
So static data, () prior to execution in the main, including memory allocation, initialization.
// low address code area and global data heap stack area // high address area
the general procedure of the newly generated dynamic data stored in the stack area, the interior of the automatic variables held in the stack area. With automatic variables tend to withdraw from functions and free up space, static data (even internal functions static local variable) is also stored in the global data area. Data in the global data area and will not exit the function and free up space.

Define static global variables have the following benefits: (only valid within this document acts)
1. static global variables can not be used in other documents;
2. Other variables can be defined in the same file name, not conflict;

 

Guess you like

Origin www.cnblogs.com/fanhua666/p/11490589.html