|
|
|
|
int a; // global static int b; // static global int func( int e ) // param { int d; // local static int c; // static local a = 1; b = 2; c = 3; d = 4; e = 5; } |
Compile with: gcc -S kinds-vars.c and take a look at the output kinds-vars.s:
.file "kinds-vars.c" .comm a,4,4 // allocate global a .local b // make name "b" localized ! .comm b,4,4 // allocate 4 bytes for variable b .text // Start of instructions .globl func .type func, @function func: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 movq %rsp, %rbp .cfi_offset 6, -16 .cfi_def_cfa_register 6 movl %edi, -20(%rbp) movl $1, a(%rip) // global var accessed through a label movl $2, b(%rip) // static global var also movl $3, c.1619(%rip) // static local var also movl $4, -4(%rbp) // local variable is in the stack movl $5, -8(%rbp) // parameter var is in the stack // %rbp is the base pointer register // Point to the base of the stack frame ! leave .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size func, .-func .local c.1619 // Make name "c" localized .comm c.1619,4,4 // Allocate static local variable c .ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2" .section .note.GNU-stack,"",@progbits |
Notice that:
|
|
|
void f( ) { int i = 1234; // Normal local (created when f() is called) static int j = 2345; // Static local (created using ds, ONCE !) i++; j++; printf( "i = %d, j = %d\n", i, j ); } int main( int argc, char* argv[] ) { f(); f(); f(); f(); f(); } |
Output:
i = 1235, j = 2346 // i does not change i = 1235, j = 2347 // j changes !!! i = 1235, j = 2348 i = 1235, j = 2349 i = 1235, j = 2350 |
How to run the program:
|
(Which will be discussed next)