Jump to content
  • Advertisement

Archived

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

Leadorn

Allocation with new

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

Hi If I want to check if new have accomplished allocation can I do like this. But in debug mode doesn''t all variables default become 0xdcdcdc. Then this code doesn''t work.....
  
if((DataItemArray = new Item *[iMAXITEMS]) == 0)

  

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Leadorn
But in debug mode doesn''t all variables default become 0xdcdcdc.

All uninitialized variables are assigned some valid in MSVC''s debug mode (what value I''m not sure, since I rarely use MSVC, much less its debug mode). That code will check for the new''s operators failure assuming that your compiler does not follow standard procedure for the failure of new, and instead returns NULL (0) by default upon failure. MSVC (6.0, at least) does have this problem.

Share this post


Link to post
Share on other sites
The ANSI (or whatever) standard says that operator new returns NULL on failure. With Microsoft''s implementation operator new throws an exception instead, so:


  
try
{
DataItemArray = new Item[iMAXITEMS];
// if you arrive here, memory allocation succeeded

ZeroMemory(DataItemArray, iMAXITEMS * sizeof(Item));
}
catch(...)
{
// failed to allocate memory

// handle error here

}


If you don''t like the exception stuff you can override operator new and delete and make it behave like you want it to

Share this post


Link to post
Share on other sites
It will return the address allocated not true or false the address........

If it fails I think it will return a NULL address.... but for making it easier to debug they use 0xdcdcdc.

Why do you answer a q that you cant answer?

Do you only post because you want to tell people what you think of Microsoft programs?

Share this post


Link to post
Share on other sites
quote:
Original post by Christian Leinen
The ANSI (or whatever) standard says that operator new returns NULL on failure. With Microsoft''s implementation operator new throws an exception instead...

Nope, you got that backwards. The new operator should throw an exception (by default), but MSVC''s new operator does not throw an exception. You can force new to not throw an exception by giving it ''nothrow''.

Share this post


Link to post
Share on other sites
quote:
Original post by Leadorn
It will return the address allocated not true or false the address...

Yes, it returns the address. NULL is an address that no variable can reside at.

quote:
Original post by Leadorn
If it fails I think it will return a NULL address.... but for making it easier to debug they use 0xdcdcdc.

In MSVC it will (should) return NULL on failure whether or not you''re in debug mode.

quote:
Original post by Leadorn
Why do you answer a q that you cant answer?

Why do you ask a question that you think people can''t answer?

quote:
Original post by Leadorn
Do you only post because you want to tell people what you think of Microsoft programs?

You''re confused.

Share this post


Link to post
Share on other sites
SOOORRRRY !!! You are right, it seems like the exception thing only happens when using the MFC

I''m not posting to tell people what i think about Microsoft. What i stated was what i believed and what i have experienced so far. I was just trying to help, nothing else. Sorry again for giving an answer with wrong information!

Share this post


Link to post
Share on other sites
quote:
Original post by Christian Leinen
I''m not posting to tell people what i think about Microsoft. What i stated was what i believed and what i have experienced so far. I was just trying to help, nothing else. Sorry again for giving an answer with wrong information!

I think he was complaining about me. My assumption is that he took the part about me ''not using MSVC often'' to be ''never'' and then thought I was merely insulting Microsoft when I was informing him about standard C++. As a note: I pointed out the ''MSVC problem'' part because that code is never valid for error checking outside of MSVC (even though it is always valid in MSVC) and I wanted to make that clear.

Share this post


Link to post
Share on other sites
quote:
Original post by fallenang3l
MSVC++ does throw a bad_alloc exception in both debug and retail builds. I''m talking about .net edition, of course.

Yeah, I mentioned ''6.0, at least'' in my first reply.

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!