VS7 NULL Anomalies

Started by
8 comments, last by AndyTang 21 years, 4 months ago
Anyone with Visual Studio 7 with this kind of strangness? Something simple: char* name = new char[10]; if( name==NULL ) return FALSE; return TRUE; The top three code will always return FALSE, cause name always seem to be NULL. This is not the case as name is created properly cause when I debug it, the address goes from 0x0000000 to something else. Unfortunately, it seem to detect that as NULL even though NULL should be 0. Anyone experienced this? Anything I can do cause its wreaking havoc with my old code from VS6.
Advertisement
Is the release build returning "False" and the debug build returning "True?"

It''s possible that since name isn''t used before it returns that the compiler is phasing the variable and all code related to it out of existance.

RomSteady - Test Locally, Test Globally, Test Early, Test Often
Michael Russell / QA Manager, Ritual EntertainmentI used to play SimCity on a 1:1 scale.
Both build return FALSE.

I cant use ''name'' cause it return sFALSE which automatically closes down the application (which is what should happens in the event of no memory being allocated).

If I remove the test and do:

char* name = new char[10];
//if( name==NULL ) return FALSE;
strcpy( name,"Alex" );
return TRUE;

It works perfect. The problem with this is that it isnt checked for memory allocation failure.
Double check if
if( name==NULL ) return FALSE; 
is written as
if( name==NULL ); return FALSE; 
. (notice the small semi-colon )

It's better not to check NULL-ness for new. I would suggest using try/catch for allocation failure.


[edited by - DerekSaw on December 2, 2002 7:49:29 PM]
"after many years of singularity, i'm still searching on the event horizon"
you wouldnt want a semicolon after the if statement, would you? the if statement is properly structured. If you put a semicolon after the parentheses, it would translate to: if (condition) do nothing. then it would always return false. Is that what you were trying to get at? I dont think thats his problem though because its right in the code he posted.
Brian J
fine with me.
it''s probably because of namespace conflict.
if you just type ''name'', just ''name'' and move your mouse over it, and it will give you a tooltip like:

string std::name(void)

if you use ''using namespace std'' that''s probably the cause.

My compiler generates one error message: "does not compile."
yeah good point! name is a commonly used identifier. are you using the standard lib?
Brian J
copy and paste the exact code. i don''t believe you would have a problem with what you''ve typed.

i''d prefer to use zero to NULL. try setting name to zero before assignment.

actually new won''t return zero unless you use new(std::nothrow) char[10];

the behaviour you''re expecting is old, prestandard (ie at least 5 years old), and basically wrong.
I found out what it was. Thanks all, you all helped a lot...

I just had a little logic problem thats all. It was working but I told it to return FALSE...

Actually, it stems from code by Jim Adams, so if your reading this Jim Adams, a little error on page 405 where you have:

if(( m_Name=new char[10] )==NULL )
strcpy( m_Name,"Jim Adams" );

[edited by - Chrominium on December 2, 2002 8:10:03 PM]
I love it when we go through all this complicated stuff and the error is really simple! At least we all learn something...but from now on post code so we can make it simpler.

Brian
Brian J

This topic is closed to new replies.

Advertisement