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
, ormap
, we may be allocating memory on the heap. If we think thatnew
will end in an exception (and therefore choose to usenew (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?