2

Here gdb does not stop at Line:4.

enter image description here

Next,

Without hitting the declaration line at Line:5, variable x is existing and initialized.

enter image description here

Next,

But here it shows out of scope (yes it should according to me).

enter image description here

Now, I have the following doubts regarding this particular instance of c program.

  1. When exactly the memory for variable x in P1() gets created and initialized?
  2. why gdb did not stop at static declaration statement in inside P1() in the first example?
  3. If we call P1() again will the program control simply skip the declaration statement?
Debashish
  • 1,155
  • 19
  • 34
  • 2
    http://stackoverflow.com/questions/30581675/what-actually-compiler-does-when-we-declare-static-variables – akira Dec 15 '16 at 06:08
  • if it gets reinitilized each time then what is the meaning of `static` check [Wiki](https://en.wikipedia.org/wiki/Static_variable) – bansi Dec 15 '16 at 06:09
  • 2
    Related: http://stackoverflow.com/q/5033627/694576 – alk Dec 15 '16 at 06:11
  • 3
    do you understand the difference between `initialization` and `assignment`? The static `x` variable is allocated at link time and initialized at program load time. This is because the variable `x` is in the file scope and not on the stack. So there is no runtime code to allocate it nor to initialize it. The first code to access it is the `+=5` statement. – user3629249 Dec 15 '16 at 06:12
  • @bansi I know it will be initialized only once, but how it will be skipped 2nd time? that is what I am interested in. – Debashish Dec 15 '16 at 06:12
  • 1
    the initialization was done at program load time, not at program run time, – user3629249 Dec 15 '16 at 06:15
  • 1
    So in P1(), for gdb declaration Line does not make sense. correct ? because no instruction there in the executable to initialize. ? – Debashish Dec 15 '16 at 06:17

1 Answers1

1

It has already been explained (in related topics linked in comments below question) how static variables work.

Here is actual code generated by a gcc for your p1 function (by gcc -c -O0 -fomit-frame-pointer -g3 staticvar.c -o staticvar.o) then disassembled with related source.

Disassembly of section .text:

0000000000000000 <p1>:
#include <stdio.h>

void p1(void)
{
   0:   48 83 ec 08             sub    $0x8,%rsp
    static int x = 10;
    x += 5;
   4:   8b 05 00 00 00 00       mov    0x0(%rip),%eax        # a <p1+0xa>
   a:   83 c0 05                add    $0x5,%eax
   d:   89 05 00 00 00 00       mov    %eax,0x0(%rip)        # 13 <p1+0x13>
    printf("%d\n", x);
  13:   8b 05 00 00 00 00       mov    0x0(%rip),%eax        # 19 <p1+0x19>
  19:   89 c6                   mov    %eax,%esi
  1b:   bf 00 00 00 00          mov    $0x0,%edi
  20:   b8 00 00 00 00          mov    $0x0,%eax
  25:   e8 00 00 00 00          callq  2a <p1+0x2a>
}
  2a:   90                      nop
  2b:   48 83 c4 08             add    $0x8,%rsp
  2f:   c3                      retq   

So, as you see there is no code for declaration of x. GDB can only break on actual machine code instruction and as there is none, it breaks on next instruction (mov), which matches line 5.

dbrank0
  • 9,026
  • 2
  • 37
  • 55