In VC++Express, can you debug (display) the contents of a static
global object within your current context, if you have not referenced
it?

Re: Debug Global Objects by Giovanni

Giovanni
Sat Oct 20 14:58:32 PDT 2007


"Randy" <gastcom@sympatico.ca> ha scritto nel messaggio
news:1192897581.068852.72550@k35g2000prh.googlegroups.com...
> In VC++Express, can you debug (display) the contents of a static
> global object within your current context, if you have not referenced
> it?

What do you mean by "static global" object?

If an object is global, but you put the "static" keyword, it becomes local
to the current .c/.cpp module, e.g.

---- [MyModule.cpp] ----

static MyClass MyClassInstance;
----

MyClassInstance is only accessible inside MyModule.cpp

Instead, if you don't put the static keyword, a global object can be
accessed from everywhere in the program.

I don't like global objects very much (they are kind of against
object-oriented design, IMHO). However, I think that you can see a global
object from the debugger... why not? Have you tried it?

Giovanni



Locally-declared static variables in optimized code. by Jeff_Relf

Jeff_Relf
Sat Oct 20 16:53:18 PDT 2007

It would help if you showed some code, Randy, like this...

You may or may not be able to debug ( view )
locally-declared static variables in optimized code.
i.e. to me, it seems fairly random.

For example, consider these settings:

Compiler switches ( VC++ 8.0 SP1, Vista Update ):
/Ob2 /Oi /FD /MT /GS- /GR- /Fo"EXE\\" /Fd"EXE\vc80.pdb"
/W4 /WX /nologo /c /Zi /TP

Linker switches:
/OUT:"EXE/Z.EXE" /NOLOGO /MANIFEST:NO /DEBUG /SUBSYSTEM:WINDOWS
/OPT:REF /OPT:NOICF /OPT:NOWIN98 /MACHINE:X86 /ERRORREPORT:PROMPT
kernel32.lib ....

with this code:
#pragma warning( disable: 4007 4127 4430 4508 )

WinMain( int, int, int, int ) {
while ( 1 ) {
static double X ;

// Breaking here, the debugger can't see what X contains.
if ( ++ X > 5 ) return ;
} }


Re: Locally-declared static variables in optimized code. by Randy

Randy
Sat Oct 20 18:10:02 PDT 2007

Thank you for responding and my apologies for the terminology butchery
and no example.


game.h

class Game{
}
extern Game *TheGame;



Interface.h

class Interface{

long GetStatus(void);
}



long GetStatus(void){
return TheGame->GetScores();
}



The call completes TheGame->GetScores() ok and Intellinsense completes
the member_function.

My questions is : In the debugger, can I not watch "TheGame" while I
am sitting on this line (return TheGame->GetScores();) ?



Re: Locally-declared static variables in optimized code. by Randy

Randy
Sat Oct 20 18:11:21 PDT 2007

Thank you for responding and my apologies for the terminology butchery
and no example.


game.h

class Game{
}
extern Game *TheGame;



Interface.h

class Interface{

long GetStatus(void);
}



long GetStatus(void){
return TheGame->GetScores();
}



The call completes TheGame->GetScores() ok and Intellinsense completes
the member_function.

My questions is : In the debugger, can I not watch "TheGame" while I
am sitting on this line (return TheGame->GetScores();) ?



Re: Locally-declared static variables in optimized code. by Randy

Randy
Sat Oct 20 18:15:08 PDT 2007

Thank you for responding and my apologies for the terminology butchery
and no example.


game.h

class Game{
}
extern Game *TheGame;



Interface.h

class Interface{

long GetStatus(void);
}



long GetStatus(void){
return TheGame->GetScores();
}



The call completes TheGame->GetScores() ok and Intellinsense completes
the member_function.

My questions is : In the debugger, can I not watch "TheGame" while I
am sitting on this line (return TheGame->GetScores();) ?



Randy, perhaps something needs to be declared =?UTF-8?Q?=E2=80=9C?= public =?UTF-8?Q?=E2=80=9D...?= I don't know. by Jeff_Relf

Jeff_Relf
Sat Oct 20 19:32:40 PDT 2007

I see no reason why you couldn't watch â?? TheGame â??
at â?? return TheGame->GetScores(); â??.
Perhaps something needs to be declared â?? public â??... I don't know.

You could turn off pre-compiled headers,
turn off any optimizations, clean your project and rebuild it.

I'm not the best person to answer your question, Randy,
and this newsgroup gets really, really quite on Saturday and Sunday,
Seattle time.


Re: Locally-declared static variables in optimized code. by Norbert

Norbert
Sun Oct 21 02:34:36 PDT 2007



Jeffâ? Relf schrieb:

> #pragma warning( disable: 4007 4127 4430 4508 )

I wonder why all of your code samples contain this #pragma warning to disable a
list of (in my view valid) compiler warnings? Wouldn't it be better to adjust
your code accordingly?

> WinMain( int, int, int, int ) {

I belive the correct WinMain signature is:

#include <windows.h>
int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)

which removes warnings 4430 and 4007.
If you do not like to include winmain for these small demo projects, you could use
int __stdcall WinMain()
instead.

> while ( 1 ) {

As a warning-free pattern for an endless loop (which is also recognised by tools
as Lint) you can use:
for (;;) {
which removes warning 4127.

> static double X ;
>
> // Breaking here, the debugger can't see what X contains.
> if ( ++ X > 5 ) return ;

Since WinMain returns an integer exit code, you should return a value, like

if ( ++x > 5) return 0;
which removes warning 4508 (or even error 2561 after fixing warning 4430 above)

> } }
>

Norbert

I wouldn't be caught dead using anything like =?UTF-8?Q?=E2=80=9C?= lint =?UTF-8?Q?=E2=80=9D.?= by Jeff_Relf

Jeff_Relf
Sun Oct 21 03:20:47 PDT 2007

As you might've guessed by now, Norbert,
I wouldn't be caught dead using anything like â?? lint â??.

Re: My code that starts like:
â?? #pragma warning( disable: 4007 4127 4430 4508 ) â??,

Why do these warnings bother you ? I'm perfectly fine with them.
I won't â?? uglify â?? my code just to accommodate them ( or anyone else ).
I code however I see fit... and that's the way I like it.

Re: My: â?? while ( 1 ) â??,

In a similar vein, I often turn-off booleans, like this:
â?? if ( Ch == '<', 0 ) â??. ( e.g. when debugging )

By the way, this is a macro I use extensively:
â?? #define LOOP while ( 1 ) â??.

If I ever had to use Java or C#,
getting a â?? macro preprocessor â?? for it
would be my first order of business.


Re: Locally-declared static variables in optimized code. by Giovanni

Giovanni
Sun Oct 21 05:44:48 PDT 2007


"Randy" <gastcom@sympatico.ca> ha scritto nel messaggio
news:1192929308.241320.283570@y27g2000pre.googlegroups.com...

> long GetStatus(void){
> return TheGame->GetScores();
> }

I've not tried your sample, but I believe you can see TheGame instance from
the debugger in this context.

Giovanni