This is the first post of the Questions&Answers series. With the tag QeA I'll collect all the relevant answer I'm posting in international forum on C/C++. Hope you'll enjoy it!
The answer is: "Static variable are faster then global and local variables." Moreover, there is no difference between global and local variables, because both are allocated in the stack and must be popped from the stack every time you use them. Let see together a practical demonstration of it!
If we consider the following simple source code:
|
|
compiling it using gcc with option -S give us the following:
|
|
Note that we used int constants 11, 22 and 33 to easily identify the fragment of assembler code of interest.
We can observe that:
- For what concern the global variable b and the local variable c, they are both popped from the stack and then the addition is performed;
- For what concern the static int variable a the compiler use the addl instruction with the absolute address of the space reserved to the variable a with the .lcomm _a,16 assembly directive.
Q.E.D. :)
Nessun commento:
Posta un commento