Memory allocation problems are seldom due to allocation requests. Because an operating system’s virtual memory space is large, allocation requests usually succeed. Problems most often occur if you are either using too much memory or leaking it. Although problems are rare, you should always check the value returned from calls to allocation functions such as
malloc(),
calloc(), and
realloc(). Similarly, you should always check whether the C++
new operator returns a null pointer. (Newer C++ compilers throw a
bad_alloc exception.) If your compiler supports the
new_handler operator, you can throw your own exception.