4

What is the best way to check the pointer return by a new operator I see following type of code. Assume I have class Test

Type 1

Test *ptr = new Test;
if ( ptr == NULL ) {
}

Type 2

Test *ptr = new Test;
if ( !ptr ) {
}

Type 3

Test *ptr = new Test;
if ( ptr == (Test*)0 ) {
}
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • 2
    Please define what you mean by "best". And what about exceptions? Are you using some special `new` that does not raise exceptions? – David Heffernan Jan 25 '12 at 09:47
  • 1
    the best one would be `if(ptr==nullptr)` but that assumes a C++11 compatible compiler. – PeterT Jan 25 '12 at 09:47
  • Are you looking at old Win32/Windows/COM code by any chance? Current C++ code throws exceptions in case of new failing; but before exceptions were widely supported, you had to check for NULL. Sometimes Windows or COM-based code will opt-out of using exceptions either to avoid porting old code bases, or just because it's sometimes easier to deal with: COM requires all exceptions to be converted to a HRESULT before being returned from an interface. See the answer to [this SO question](http://stackoverflow.com/a/550457/660175) for some more details. – BrendanMcK Jan 25 '12 at 09:58

4 Answers4

10

You do not check new for null it throws an exception std::bad_alloc in case it fails.
So you handle the exception.

Ofcourse, the above rule applies if you are not using the nothrow version of new.

 try
  {
    Test *ptr = new Test;
  }
  catch (std::bad_alloc &e)
  {
    cout << "new Failed";
  }
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • This feels more like a comment than an answer to the question that was asked – David Heffernan Jan 25 '12 at 09:49
  • 1
    Actually it is the correct answer to the question because there will be NO case where Test * ptr = new Test; will have a NULL or a 0 value. – Ahmed Masud Jan 25 '12 at 09:55
  • @DavidHeffernan: Checking return value of pointer returned by `new` is useless.You need to handle the exception thats about it.If you are going to suggest the OP to check for return value of `new` then congratulations you just lost one of the most important edge `new` provides over `malloc`.Given, that I don't see how it does not answer the question. – Alok Save Jan 25 '12 at 09:55
  • Unless OP is using a `new` that doesn't throw. – David Heffernan Jan 25 '12 at 09:57
  • @DavidHeffernan: Did you miss *Ofcourse, the above rule applies if you are not using the nothrow version of new.* part of the answer??? – Alok Save Jan 25 '12 at 09:58
  • addendum: actually my earlier comment is incomplete it WILL return NULL and you can check for it with nothrow. As in `Test *ptr = new (nothrow) Test;` in which case you can check for a nullptr. Or if the new operator is overridden for the Test class to catch the throw and return 0 (?) – Ahmed Masud Jan 25 '12 at 09:59
  • No I didn't. I guess what I missed was the bit where you addressed the question that was asked for the `nothrow` case. I still can't see it. ;-) Of course, the question is a bit meaningless. What does "best" mean for example? – David Heffernan Jan 25 '12 at 10:00
1

By default new should throw an bad_alloc exception, so no checks should be necessary.

On the other hand VC6 do return 0 on a bad_alloc in that case Type 3 should be perfectly fine in cases where you do not have nullptr (which is in the C++11 standard)

Another way to be able to test for NULL is to call new with std::nothrow:

ptr = new(std::nothrow) Test();

In any case remember that you do not have to catch every bad_alloc where it is thrown. You are better off catching it where you can respond to it (free memory or die gracefully).

daramarak
  • 6,115
  • 1
  • 31
  • 50
0

Assuming your allocator/implementation returns 0 instead of throwing, I think this is the best:

Test* const ptr(new Test);
if (0 == ptr) {
// uh-oh
}

0 is common in C++, and you won't make the mistake of assigning the pointer with the constant on the left.

If it's local to the if, you may like this:

if (Test* const ptr = new Test) {
// use it
}
justin
  • 104,054
  • 14
  • 179
  • 226
0

Always try lhs to be constant for == operator

 Test *ptr = new Test;
 if ( NULL == ptr ) { } 

Here, by mistake if you write = in stead of ==, it will give compile error.

But if you write if( ptr = NULL), It will not give any error, NULL will be assigned to ptr

user966379
  • 2,823
  • 3
  • 24
  • 30