Recently I discovered that there're still a number of people doesn't know what does the keyword 'static' do exactly!!! Well then, here comes my tutorial of 'static'.
In brief, the static keyword does only one thing, it tells the program to allocate and maintain a number of fixed memory space to store the variable before the program start to execute, and treat the scope of static variables as the ordinary ones.
Take the following code segment for example.
[ C/C++ ]
static int variable01 = 10;
void testFunc()
{
static char variable02 = 'd';
variable01 = 20;
variable02 = 'b';
return;
}
In this C/C++ code segment, there are two static variables, variable01 and variable02. During compilation, the compiler will preserve two spaces for the variables, one is 4 bytes long, and the other is 1 byte long. When the program executes, the program will not do any further space allocation but use the preallocated spaces instead.
The following is the assembly code segment generated from the code segment above. Line 1, and line 5 in C/C++ code segment are covert into line 13, 14 and line 16, 17 in assembly code segment respectively. And the variable assignment statements of line 6, and line 7 are covert into line 5 and line 6, too.
[ GNU Assembly (GAS) ]
As you can see, after being compiled, the declaration of the static variables are isolated from the function body and have fixed memory addresses. All things the testFunc does is to assign the value into the spaces corresponding to the two static variables.
With this schema, the static variables now has fixed spaces that are instantiated along with the startup of the program. This is the reason why somebody would say that the static variable is existed at the program starts and has exactly the same lifetime as the program has.
So far are the cases of static variables. But how about the static member functions and member variables of a class ? The answer is .... the class static functions and static variables are implemented using the similar (even the same) schema!!!
Here's the keywords related to the article.
[GNU Assembly], [CODE Segment and DATA Segment]
[ GNU Assembly (GAS) ]
_testFunc: func_begin: pushq %rbp movq %rsp, %rbp movl $20, _variable01(%rip) movb $98, _variable02(%rip) popq %rbp ret func_end: .section __DATA,__data .align 2 _variable01: .long 10 _variable02: .byte 100
As you can see, after being compiled, the declaration of the static variables are isolated from the function body and have fixed memory addresses. All things the testFunc does is to assign the value into the spaces corresponding to the two static variables.
With this schema, the static variables now has fixed spaces that are instantiated along with the startup of the program. This is the reason why somebody would say that the static variable is existed at the program starts and has exactly the same lifetime as the program has.
So far are the cases of static variables. But how about the static member functions and member variables of a class ? The answer is .... the class static functions and static variables are implemented using the similar (even the same) schema!!!
Here's the keywords related to the article.
[GNU Assembly], [CODE Segment and DATA Segment]
No comments:
Post a Comment