Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check memory allocation failures with new operator?

Just recently I switched the language of my project to use C++ from C. With C, I used malloc and after that I check if malloc was successful but with C++, I use 'new' to allocate memory and I would like to know how you would normally check the memory allocation failure.

From my google search, I saw nothrow like the following.

char *buf = new (nothrow)char[10]; 

I also saw the following.

try{} catch(bad_alloc&) {} 

But what about the following? I am using some of chrome library routines to use smart pointers.

For instance, I have the code as follows.

scoped_array<char> buf(new char[MAX_BUF]); 

It is great to use smart pointers but I am just not sure how I should check if the memory allocation was successful. Do I need to break into two separate statement with nothrow or try/catch? How do you normally do these checks in C++?

Any advice will be appreciated.

like image 641
istudy0 Avatar asked Jul 26 '11 16:07

istudy0


People also ask

How do I know if my memory allocation failed?

We can also use the Malloc function to check for errors about memory allocation. When a malloc method finds itself unable to allocate memory, it usually returns NULL. You can also through an error message if the allocation got failed upon managing the pointers.

What happen if new operator fails to allocate the memory?

Answer: When we allocate memory from heap dynamically in a C++ program using new operator, the program crashes when memory is not available, or the system is not able to allocate memory to a program, as it throws an exception. So, to prevent program crash, we need to handle the exception when memory allocation fails.

Does the new operator allocate memory?

new operator. The new operator denotes a request for memory allocation on the Free Store. If sufficient memory is available, a new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.


2 Answers

Well, you call new that throws bad_alloc, so you must catch it:

try {     scoped_array<char> buf(new char[MAX_BUF]);     ... } catch(std::bad_alloc&)  {     ... } 

or

scoped_array<char> buf(new(nothrow) char[MAX_BUF]); if(!buf) {    //allocation failed } 

What I mean by my answer is that smart pointers propagate exceptions. So if you're allocating memory with ordinary throwing new, you must catch an exception. If you're allocating with a nothrow new, then you must check for nullptr. In any case, smart pointers don't add anything to this logic

like image 194
Armen Tsirunyan Avatar answered Sep 21 '22 22:09

Armen Tsirunyan


I hate to say it, but IMO, you're going in the wrong direction (and, unfortunately, the other answers you've gotten haven't really pointed you in the right direction either).

Rather than choosing between different varieties of smart pointer and/or normal vs. nothrow variants of new, you should probably take at least two more steps back from what you're doing, and replace your manually-managed dynamic data structures with collections. This may not always be the right choice, but at least in my experience, it's the right way to go a lot more often than not. The standard library has a number of possibilities (vector, deque, list, set, etc.), and chances are pretty good that you can use one of them rather than dealing directly with new and company at all.

By default, those will use an allocator that ends up using the normal (throwing) variant of new. You, therefore, normally want to put most code in a try block at a fairly high level, and have a catch clause that deals with having run out of memory there.

When/if you do need to deal with allocating memory directly, chances are pretty good that you still want to provide an interface similar to that of the standard containers in the library so it'll work with the normal algorithms and iterators. Your initial experience using the existing containers will pay of well when you get to this point, even though it may be a ways down the road.

like image 39
Jerry Coffin Avatar answered Sep 20 '22 22:09

Jerry Coffin