Archived

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

Allocation with new

This topic is 5891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
quote:
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 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 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.

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

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 on other sites
quote:
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:
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:

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

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

You''re confused.

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 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 on other sites
MSVC++ does throw a bad_alloc exception in both debug and retail builds. I''m talking about .net edition, of course.

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.

1. 1
2. 2
frob
17
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• Forum Statistics

• Total Topics
632140
• Total Posts
3004373

×