[Exceptional C++ Style] Item 22: To new, Perchance to throw, Part 2: Pragmatic Issues in Memory Management

Adrian Fagg adrianf at evaluategroup.com
Thu Jan 20 06:04:28 EST 2005


> -----Original Message-----
> From: effective-cpp-bounces at accu.org
> [mailto:effective-cpp-bounces at accu.org]On Behalf Of Balog Pal
> Sent: 20 January 2005 10:20

A couple of notes:

> Herb indeed writes that -- though it's not entirely correct. Memory
> allocation can fail on those systems too, as the address space is not
> infinite, and the memory manager may run out of virtual space
> even if no
> memory was actually put into use ever.

One possible cause of allocation failure is the manifestation of a bug or
runtime fault, e.g. trying to allocate memory for a string from a random
size value or as a consequence of external attack (i.e. another kind of
bug). Then all probably is lost (see below).

> > Recovering from memory exhausting sensibly is difficult -
> one of our most
> > basic resources has run out after all - and unwinding the stack is
> > generally the best option.

It's possibly worth emphasizing that said stack unwinding often allows an
application to continue to run without problems. I've often heard it said
that memory allocation failures are best handled by termination of the
program as all is lost... For many applications, failing the operation
cleanly and being ready to perform the next one is perfectly acceptable
behaviour. Provided that your libraries and application code all behave
properly, that is.

Regards,

Adrian

Work: mailto:adrianf at evaluategroup.com
Home: mailto:adrian.fagg at ntlworld.com

Pretty pictures: http://homepage.ntlworld.com/rbyadf/




More information about the Effective-cpp mailing list