Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AndyTang

VS7 NULL Anomalies

This topic is 5683 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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!