The main introduction of the next title a few kinds of variables, is also relatively easy to mix four variables, each of the variables should have their own unique part, only the value of existence;
|
Storage area |
Scope and time to live |
Local variables |
Stack area |
Local; within the scope |
Global variables |
Static Storage Area |
Global, can be used in multiple source files; During the entire program execution |
Static local Variables |
Static Storage Area |
The entire program is executed. |
Static global variables |
Static Storage Area |
Global, can only be used in this document; During the entire program execution |
So, in fact, each type of variable will have its own unique place, for local variables, can be used in local scope, the rest of the site is not accessed, the variable has a certain protection, and every time into the execution will be the same,
Its value does not need to be kept, and if you want to work on the local, and you want the next call to use the variable can still save the last value, then you can use static local variables.
Static storage: A special area in memory where a variable is stored in a static store, allocated space at the time the program compiles, and then the lifetime is the entire program execution.
For C + + memory partitioning, you can refer to the detailed description of the C + + heap, stack, and static data area
Divided into five zones, heap, stack, free storage, static storage, and constant storage area.
Or the main introduction is How to view the stored data, the main description of where to store? When is it assigned? (win8+vs2012)
Breakpoints set at the beginning of the execution, you can find that C, D, a have been allocated space, and all in the same area (but I can not prove that this is static storage), it is possible to find that the area is initialized is 0, so it is not the stack area, and can be seen as four byte integer, and according to C, D, The order of a is assigned.
Then see the allocation of local variables, should be in the stack area, and for the stack section can know if the definition of a variable is not assigned to the initial value, generally in the VC will be filled with 0xCC, so for the stack area is generally there will be many filled with 0xCC area:
You can see this area from disassembly, the surrounding part is not using (initialize) The area is filled 0xCC, this is a feature of the stack area, so it is basically able to explain that the local variable is actually placed in the stack area.
Specific memory information allocation can be referenced: Memory area of the division and the stack of detailed
In fact, the first is to call through the positioning function, that is, under Windows, if you allocate space in the heap, the HeapAlloc function will be called, the allocation in the local variable stack will call the LocalAlloc function, but do not know if the debug location into the function call, looking past the Daniel guidance ^^
Introduction to C + +-several types of variables (local, static, static global, global)