Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Twilight zone... if (s) same as if (!s)!!!???!!!??? 1

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
I am building a C library and I experience some awfully weird problems on the testing program...

I have an if () test and the program gets through no matter what the condition is.

below is the code:
Code:
s = ExistElementInCollection(col,id);
if (s)
   return false;

Well, by stepping through the code (MSVC 6) I can clearly see that:
s = 0x00000000.
Now, either that does not mean NULL and I've gone fishing, either something weird is going on with the compiler.

Even more, no matter what condition I put there:
Code:
if (s != NULL), if (s == NULL), if (s), if (!s)
THE SAME THING HAPPENS! false is returned!

Even weirder, one step before, in the calling function, I have a redundant test, on the same variables:
Code:
if (ExistElementInCollection(col,id))
   do_something
else
   call_buggy_code
and the call_buggy_code gets called.

I am petrified at this... Does anybody has any clue about this????
This started to occur a few minutes ago and it keeps rolling.

I am about to publish this on Guiness Book of Most weird
things on the planet...

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
That is very strange. The only thing I can think of is that the compiler thinks that there is a semi-colon following the if and therefore the return is not really part of the if.

i.e.


if(s);
return false;

always returns false. I am pretty sure this would happen. I know I have done while loops with just a semi-colon and it works like that for those. I suggest placing your cursor after the semicolon at the end of the function call,

s = ExistElementInCollection(col,id); <--- this ;

highlight past the if conditional, and rewrite it using brackets to be sure. Put an else in as well. Put some output debugging statements within the if and the else. Then, for poops and giggles, change the s to zero and see if you get into the if. If it really is an issue with your compiler, you will always get into the if. IF (no pun intended) that is the case, i suggest a reinstall.

Matt
 
Thanks Matt for your fast response...

You know, it seems that no matter how much you learn and get experienced, every now and then you hit the wall with something like this.

I started laughing when I saw your reply - in a good feeling-, probably because I was hoping that'll be the problem; I was using this as a reminder technique when writing large code.
But, if it was this way, I was to get a warning from the compiler, which I didn't.

So, the dusk gets more and more spooky around this. I did restarted the computer, got MSDEV crashing several times after restart (problems with the IDB file created), restarted the computer again anso... got again the same issue.

...

And voila!!!! The adding the else solved the issue!!!
Oh, my GOD!
I was using a MACRO on the positive branch of the IF - the first time I was using it in a conditional branch- and the macro lacked the curls!!! Jeesus!!!

You can reproduce this, for your amusement (or play a scam on somebody as it feels a bit subtle - at least, IT REALLY GOT ME!):

Code:
#define return_err_code(code)         last_err = code; 	return code; 


... somewhere in main:

if (anyTest)
   return_err_code(10)

... other instructions

You'll only notice that something is wrong when adding the else.

Well... what a fine way to start a week-end...
DUH!
Here's the star for you Matt. You saved my Central Unit from flying out the window :)

Best regards,
--Razvan [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Macro's can really bother you like that. I'm using a kind of code-standard for macros that you might find usefull. The thing is that it uses a different naming scheme for macros than for ordinary functions.

E.g.

#define M_SomeMacro(parm1,parm2,parm3) { ... }

Preceeding a macro name with M_ will at least help you in detecting _when_ you're using a macro. I may still have a strange / faulty behaviour, but at least you can tell it's a macro...

Pieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top