GDB problem accessing static variables in C

Hi,
Can anyone explain this please..... This is on AIX with GDB (tried a few versions). It only happens when program compiled in 64 bit and only with static variables....

A simple test program...

### snip
#include <stdio.h>

main() {
static int n;

n = 6;

printf("hello %d\n", n);
n=7;
}
### snip

64 bit compile/link....

$ export OBJECT_MODE=64
$ cc -g -o hello hello.c
$ gdb ./hello
(gdb) b 8
Breakpoint 1 at 0x1000004a4: file hello.c, line 8.
(gdb) r
Starting program: /src/kiwi_7.94_01oct2014/hello

Breakpoint 1, main () at hello.c:8
8 printf("hello %d\n", n);
(gdb) p n
Cannot access memory at address 0x10000708
(gdb)

If you take off the "static" on line 4, it works fine.

Also it works fine in 32 bit mode.

By your code, I'm guessing you want to use 'static' in order to be able to count the number of iterations of your function. For others in the forum, a static variable will remain in memory when a function ends, otherwise the variable ends with the termination of the function. When you do the compile add '-fsanitize=address' to get more information about the type of memory overflow that is occurring on exit.

Come back and let the community know what your solution was. Thanks! Keith

2 Likes
Moderator comments were removed during original forum migration.

This topic was automatically closed 58 days after the last reply. New replies are no longer allowed.