Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB unused variable

Is it possible to get the value of unused variable using GDB? Is there some configuration for GCC so that the garbage value of the unused variable will be shown not 'optimized out'?

c file:

#include<stdio.h>

void main()
{
    int x;
    int y;
    printf("value of x: %d",x);

}

In the gdb I want to get the garbage value of variable y.

(gdb) run
Starting program: /home/charmae/workspace/AVT/a.out 

Breakpoint 1, main () at file4.c:7
7       printf("value of x: %d",x);
(gdb) info locals
x = 2789364
(gdb) p y
$1 = <optimized out>
(gdb) p x
$2 = 2789364
like image 877
charmae Avatar asked Nov 21 '11 00:11

charmae


1 Answers

It has nothing to do with GDB. The entity that optimized that variable out is the compiler (probably GCC in your case). You might force it to keep it by declaring the variable as volatile

A better question is - why are you trying to do?

like image 50
gby Avatar answered Oct 09 '22 22:10

gby