Jump to content
  • Advertisement
Sign in to follow this  
beebs1

Empty Functions

This topic is 4030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, Does anyone know if MSVS 2005 will just omit any function calls to empty functions when it compiles a program? I know it's a bit of a strange question - the reason behind it is I'd like to take advantage of the OutputDebugString() function like so:
void DebugOut( const wchar_t *format, ... )
{

    #ifdef _DEBUG

    // use va_start() etc. to build the string

    OutputDebugString( msgbuf );

    #endif

}
I just want to check it won't cost me anything in a release build. Thanks for any help [smile]

Share this post


Link to post
Share on other sites
Advertisement
You should be using #Ifdef _Debug on the calls to Debugout, not on the debug function. From my understanding there will still be a call to the function even though it is empty.

theTroll

Share this post


Link to post
Share on other sites
An empty inline function is ignored, yes. If it isn't inline, the linker may choose to ignore it, but it's not certain.

The arguments to the function will still be evaluated if there is any chance that they have a side-effect.

Share this post


Link to post
Share on other sites
OK, thanks. I can inline it, but I suppose there's no guarantee it'll actually be inlined by the compiler. Coudld you suggest another method?

I was going to use it like this:

try {
// all these may throw
pfnInitRenderer( ... );
CreateWindow( ... );
AdjustWindow( ... );

// etc...
}
catch( const wchar_t *pReasonForThrow )
{

// this generates warning C4101 - uninitialized local variable
#ifdef _DEBUG
DebugOut( L"Error: \s. Renderer::Initialize() fails.\n" );
#endif

}


I could disable the warning, but I'd much rather not have to, I guess I'm strange like that.

Thanks again.

Share this post


Link to post
Share on other sites
Well, you are weird about that warning deal [smile] Have you corrected what was causing it? In practice, getting this warning because of a conditional compilation of a logging statement is bad news.

Share this post


Link to post
Share on other sites
I prefer the word 'special' [smile] But more seriously...

Sorry - C4101 is an unreferenced local variable, it's caused because pReasonForThrow is never used in the release build. I guess I'll have to deal with an empty function call for now - at the end of the day, if an exception's been thrown anyway a tiny bit more overhead won't make a difference. It just seems a bit ugly.

Thanks for your help.

Share this post


Link to post
Share on other sites
I'm assuming I'm well off-base here, but since the preprocessor is just straight text-replacement, couldn't you go with:

#ifdef _DEBUG
try {
#endif
// all these may throw
pfnInitRenderer( ... );
CreateWindow( ... );
AdjustWindow( ... );

// etc...
#ifdef _DEBUG
}
catch( const wchar_t *pReasonForThrow )
{
DebugOut( L"Error: \s. Renderer::Initialize() fails.\n" );
}
#endif


and, outside debug mode, let the exception propagate (hopefully) to something that can handle it, rather than pretending to handle it -- which seems a bit messy, to me? Although, I guess, you then end up sprinkling '#ifdef'/'#endif' all over the place.

(Woah, that's one serious run-on sentence)

Share this post


Link to post
Share on other sites
or just:


#ifdef _DEBUG
#define DebugWhatever( arguments )
MyDebugFunction( arguments )
#else
#define DebugWhatever( arguments ) ""
#endif



my syntax is horribly off, but i don't use macros frequently enough... but you get the idea. In debug configuration your call to the debug function gets written in to the code, if not debug then it just gets changes to whitespace.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
or just:

*** Source Snippet Removed ***

my syntax is horribly off, but i don't use macros frequently enough... but you get the idea. In debug configuration your call to the debug function gets written in to the code, if not debug then it just gets changes to whitespace.

-me


Close enough, just #define it to nothing:

#ifdef _DEBUG
#define DebugWhatever( arguments ) \
MyDebugFunction( arguments )
#else
#define DebugWhatever( arguments )
#endif


Share this post


Link to post
Share on other sites
I'd recommend using a macro to conditionally compile out the call.

#ifdef DEBUG && WIN32
#define WriteLog_(...) ::OutputDebugString(__VAR_ARGS__)
#else
#define WriteLog_(...)
#endif // DEBUG && WIN32

Variable argument macro's dont work on all compilers so there are other tricks you can do to support them if you need to make this work on multiple compilers.

Also, exception handling is a form of error management so having an empty catch in release doesn't sound like the best idea to me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!