C++ exceptions and memory allocation failure -- Wu Yongwei

C++ exceptions are habitually disabled in many software projects. A related issue is that these projects also encourage the use of new (nothrow) for fear of exceptions. Is it good practice or bad practice? Also, how likely are we to really encounter out-of-memory errors? I will investigate on this topic and discuss strategies to deal with memory issues.

C++ Exceptions and Memory Allocation Failure

By Wu Yongwei

From the article:

In fact, every time we initialize or modify a string, vector, or map, we may be allocating memory on the heap. If we think that new will end in an exception (and therefore choose to use new (nothrow)), exceptions may also occur when using these mechanisms. In a program that has disabled exceptions, the result will inevitably be a program crash.

Ignoring abnormal scenarios like allocating more memory than the physical memory size at a time (which would likely be a logic error in the program), can a reasonable program still experience memory allocation failures?

Add a Comment

You must sign in or register to add a comment.

Comments (0)

There are currently no comments on this entry.