Archived

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

Checking memory allocation on C++

This topic is 5043 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. Just a few simple questions: Checking the results of memory allocation with malloc or new is always a good practice. But how far should it go? Always check? Check only on debug versions (using assert, #ifdef)? Never check (if memory allocation is failing chances are everything else will fail too)? And what if we''re talking about game programming? Although checking a pointer is computationally irrelevant, checking millions of pointers isn''t (oh, and the code gets even uglier). Now about C++''s ''new'' keyword: Does it just return null on error or does it throw an exception of some kind? Thanks in advance. Blew

Share this post


Link to post
Share on other sites
The normal version of the new operator will throw a std::bad_alloc exception if it can''t allocate memory. The nothrow version will return 0.

Share this post


Link to post
Share on other sites
quote:
Original post by Blew
Checking the results of memory allocation with malloc or new is always a good practice.

No. Checking the result of malloc() in C is good, because C doesn''t have exceptions. Checking the result of new in C++ is generally unneeded, because if it fails it''ll throw an exception. Instead, you should be prepared to catch such an exception.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Without voicing an opinion of my own, one way or another, I''ll throw this one out for discussion: One of my professors thinks that it''s generally a waste of time to check for return values on malloc because if memory allocation fails, there''s nothing you can do about it, anyway.

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
Without voicing an opinion of my own, one way or another, I''ll throw this one out for discussion: One of my professors thinks that it''s generally a waste of time to check for return values on malloc because if memory allocation fails, there''s nothing you can do about it, anyway.


But imagine you''re trying to load a file too big to fit in memory. You could:

a) abort and ask for another file
b) load a smaller portion of the file
c) do nothing and probably give incorrect values to the user

Ok, maybe in PC''s chances are that won''t happen very often but you might be coding a library that could be ported to, say, mobile platforms, where memory limitations are a reality.


Blew

Share this post


Link to post
Share on other sites
if you didnt check and then went on to use that point to memory you would probably cause the program to crash, and if there is one thing a user hates its a crashing application.
so, better to check the return and do a controlled exit (letting the user know what happened if possible) than just letting everything fall over.

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
One of my professors thinks that it's generally a waste of time to check for return values on malloc because if memory allocation fails, there's nothing you can do about it, anyway.


There could be plenty you can do about it depending upon the circumstances.

"How you ever got to teach a class in anything is amazing"

quote:
he normal version of the new operator will throw a std::bad_alloc exception if it can't allocate memory. The nothrow version will return 0.

Remember not all compilers comply by throwing an exception on memory failure. Check what action yours takes.

[edited by - petewood on February 23, 2004 8:32:42 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
Without voicing an opinion of my own, one way or another, I'll throw this one out for discussion: One of my professors thinks that it's generally a waste of time to check for return values on malloc because if memory allocation fails, there's nothing you can do about it, anyway.


I'm very happy to see a lot of echos of my thoughts on this.

W.T.F.!?

This mantality is exactly how shit programs are written.

Besides there is ALWAYS something you can do, exit cleanly damn it!

Ask aformentioned professor why he thinks this, might be interesting.

*edit* so I'm not a total threadjack, in general (imnsho) it is good practice to check all return types on everything unless you are writting speed critical code (most isnt), the few ifs will slow you down a itsy bit, but might also save your sanity.

[edited by - FeralOfFireTop on February 23, 2004 8:45:42 AM]

Share this post


Link to post
Share on other sites