Skip to main content
gamedev.net
🔒 Locked

Weird compiler bug...?

Started by fuchiefck Oct 25, 2005 at 3:30 AM 3 replies 1.2k views
Original Post
fuchiefck
fuchiefck
Hi all, I happened to discover something weird.... I am using VC .NET 2003... My code looks something like the following:

double MyFunction()
{
  if( nValue == true )
  {
    //do something
  }
  else
  {
    return nSomeOtherValue;
  }
}
This code is wrong i know... I should put the return statement outside the if statement, or have a return in both conditions... but somehow the code compiled... and when i ran it, the code stepped into the if block (assume nValue = true), but afterwards, it traced into the else block..?! Is this some kind of bug with the compiler? or does the compiler force the program to step to the return statement?? thanks very much in advance...
-fuchiefck----------------------------------------------------------"Inside the world that you as a programmer or developer create, you are God" - Zerbst & Duvel
mirex
mirex
I'd say that this is a compiler bug, if you described it correctly.
Put trace printouts inside to see whether choice was chosen.

You are right that every possible code path should return a value, if it is not done this way the compiler should output a Error or at least Warning.
haegarr
haegarr
I've no experience w/ VC, but I'll think compiler bugs at this level are seldom (but not impossible, of course). Such a problem may arise if the binary and the source are out of sync. Are you sure you have actually compiled all belonging stuff just before debugging? If there's no return statement in the THEN branch, then the compiler may have stopped compiling w/ an error, so that the object file will not be up-to-date!

Quote:
Original by fuchiefck
or does the compiler force the program to step to the return statement??

No. That would be against specification.
gumpy
gumpy
the compiler should give a warning that not all control paths return a value. if the nValue is true it will not step into the else block and the returned value will be garbage.
This space for rent.
dave
dave
Do a clean and rebuild and see if it still happens.

ace

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.