Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* printing static const variables
@ 2009-07-16 16:16 Eric Gorr
  2009-07-16 16:21 ` Tom Tromey
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Gorr @ 2009-07-16 16:16 UTC (permalink / raw)
  To: gdb

I have a function which looks like:

Boolean TestFunction( void )
{
	static const int doubleByteDegreeMark = 42;
	static int anotherVar = 12;
	
	
	return ( doubleByteDegreeMark == 42 && anotherVar == 12 );
}

I set a breakpoint inside of this function and when it stops at this  
breakpoint, I do 'info locals' and it prints out:

(gdb) info locals
anotherVar = 12

if I try to print out the value of doubleByteDegreeMark, it gives me  
the error message:

(gdb) print doubleByteDegreeMark
No symbol "doubleByteDegreeMark" in current context.

So, my (probably really simple) question is how can I print out the  
value of doubleByteDegreeMark from GDB?

Thank you.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: printing static const variables
  2009-07-16 16:16 printing static const variables Eric Gorr
@ 2009-07-16 16:21 ` Tom Tromey
  2009-07-16 16:35   ` Eric Gorr
  0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2009-07-16 16:21 UTC (permalink / raw)
  To: Eric Gorr; +Cc: gdb

>>>>> "Eric" == Eric Gorr <mailist@ericgorr.net> writes:

Eric> 	static const int doubleByteDegreeMark = 42;

Eric> if I try to print out the value of doubleByteDegreeMark, it gives me
Eric> the error message:

Eric> (gdb) print doubleByteDegreeMark
Eric> No symbol "doubleByteDegreeMark" in current context.

Eric> So, my (probably really simple) question is how can I print out the
Eric> value of doubleByteDegreeMark from GDB?

You didn't say how you compiled this, or what platform you're on.

Probably the compiler optimized this variable away.  If so, there's no
way to print it.

You can try dumping the debuginfo for your program to find out.  How to
do that is system-dependent, though.

Tom


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: printing static const variables
  2009-07-16 16:21 ` Tom Tromey
@ 2009-07-16 16:35   ` Eric Gorr
  2009-07-16 16:56     ` Tom Tromey
  2009-07-16 17:28     ` Grant Edwards
  0 siblings, 2 replies; 5+ messages in thread
From: Eric Gorr @ 2009-07-16 16:35 UTC (permalink / raw)
  To: gdb

 > You didn't say how you compiled this, or what platform you're on.
 >
 > Probably the compiler optimized this variable away.  If so, there's  
no
 > way to print it.

Hello Mr. Tromey.

Thank you for your comments.

I still see the same problem if change the test function to:

Boolean TestFunction( int val )
{
	static const int doubleByteDegreeMark = 42;
	static int anotherVar = 12;
	
	
	return ( val == doubleByteDegreeMark || val == anotherVar );
}

Now, I suppose it is still possible that the compiler will have simply  
optimized this to:

Boolean TestFunction( int val )
{
	static int anotherVar = 12;
	
	return ( val == 42 || val == anotherVar );
}

or something.

In any case, I am using Xcode 3.1 under Mac OS X 10.5

This is the debug build with all optimizations turned off.


Under other circumstances, should 'info locals' display  
doubleByteDegreeMark? Should I be able to print doubleByteDegreeMark?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: printing static const variables
  2009-07-16 16:35   ` Eric Gorr
@ 2009-07-16 16:56     ` Tom Tromey
  2009-07-16 17:28     ` Grant Edwards
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2009-07-16 16:56 UTC (permalink / raw)
  To: Eric Gorr; +Cc: gdb

>>>>> "Eric" == Eric Gorr <mailist@ericgorr.net> writes:

Eric> In any case, I am using Xcode 3.1 under Mac OS X 10.5
Eric> This is the debug build with all optimizations turned off.

Eric> Under other circumstances, should 'info locals' display
Eric> doubleByteDegreeMark? Should I be able to print doubleByteDegreeMark?

I'm afraid you will have to ask Apple.  The answer is
compiler-dependent.  Also, they have their own fork of gdb, about which
I know little.

Tom


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: printing static const variables
  2009-07-16 16:35   ` Eric Gorr
  2009-07-16 16:56     ` Tom Tromey
@ 2009-07-16 17:28     ` Grant Edwards
  1 sibling, 0 replies; 5+ messages in thread
From: Grant Edwards @ 2009-07-16 17:28 UTC (permalink / raw)
  To: gdb

On 2009-07-16, Eric Gorr <mailist@ericgorr.net> wrote:

> I still see the same problem if change the test function to:
>
> Boolean TestFunction( int val )
> {
>   static const int doubleByteDegreeMark = 42;
>   static int anotherVar = 12;
> 	
>   return ( val == doubleByteDegreeMark || val == anotherVar );
> }
>
> Now, I suppose it is still possible that the compiler will have simply  
> optimized this to:
>
> Boolean TestFunction( int val )
> {
> 	static int anotherVar = 12;
> 	
> 	return ( val == 42 || val == anotherVar );
> }
>
> or something.

More likely it'll be optimized to

Boolean TestFunction( int val )
 {
   return ( val == 42 || val == 12 );
 }

There's no requirement that the compiler have a setting that
doesn't optimize your function to that shown above.  

Since anotherVar isn't visible outside TestFunction, and isn't
assigned to inside TestFunction, the compiler is free to treat
it as a constant.

-- 
Grant Edwards                   grante             Yow! You were s'posed
                                  at               to laugh!
                               visi.com            


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-16 17:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-16 16:16 printing static const variables Eric Gorr
2009-07-16 16:21 ` Tom Tromey
2009-07-16 16:35   ` Eric Gorr
2009-07-16 16:56     ` Tom Tromey
2009-07-16 17:28     ` Grant Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox