Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why gdb show wrong variable value? [duplicate]

Tags:

c

gdb

I have simple program:

#include <stdio.h>

void func(int i) {
    i = 1;
    printf("%d\n", i);
}

int main(int argc, char *argv[]){
    func(0);
    return 0;
}

and now:

gcc test.c -g -o test

gdb test
(gdb) b main
Breakpoint 1 at 0x400543: file test.c, line 9.
(gdb) run
Starting program: /tmp/test 

Breakpoint 1, main (argc=1, argv=0x7fffffffe458) at test.c:9
9       func(0);
(gdb) s
func (i=0) at test.c:4
4       i =1;
(gdb) p i
$1 = 0
(gdb) n
5       printf("%d\n", i);
(gdb) p i
$2 = 0
(gdb)

Program works fine, shows "1", but why gdb shows me "0" value?

Debian wheezy.

I observed that on gcc-4.7, gcc-4.6. On gcc-4.4 all is ok.

like image 301
Nips Avatar asked Mar 23 '23 18:03

Nips


1 Answers

This is a bug that is fixed if you compile with -fvar-tracking. Your question is a tighter version of this SO question, which references a bug report on GCC 4.8.0 suggesting the above compile flag.

like image 51
David Duncan Avatar answered Apr 02 '23 03:04

David Duncan