That is incorrect. The pointer is assigned whatever random value is at that memory location. For example char *ptr; the address of ptr is whatever is at that memory location on the stack (assuming its not a global variable). It is never auto set to NULL by the compiler in either C or C++ languages.

But my C++ Book says so, by the way, why are we otherwise checking whether an assignment has failed or not using if(ptr == NULL) ?

Recommended Answers

All 15 Replies

But my C++ Book says so, by the way, why are we otherwise checking whether an assignment has failed or not using if(ptr == NULL) ?

You misunderstood your book. Better read it again. Maybe the book is talking about smart pointers

commented: You were right, actually I don't have to doubt about what you say: You're just *always* right :) +3

You misunderstood your book. Better read it again. Maybe the book is talking about smart pointers

Yeah, you were right, in the book they we're talking about dynamic memory allocation with the new operator, and when allocation with new fails it returns a special pointer value: NULL , otherwise the pointer is pointing to the newly assigned memory :)

Yeah, you were right, in the book they we're talking about dynamic memory allocation with the new operator, and when allocation with new fails it returns a special pointer value: NULL , otherwise the pointer is pointing to the newly assigned memory :)

Either you are reading a very very old book or its just plain wrong. new operator does not return NULL, it throws an exception and leaves the pointer unchanged.

mplementation

In compilers conforming to the ISO C++ standard, if there is not enough memory for the allocation, the code throws an exception of type std::bad_alloc. All subsequent code is aborted until the error is handled in a try-catch block or the program exits abnormally. The program does not need to check the value of the pointer; if no exception was thrown, the allocation succeeded. The implemented operations are defined in the header <new>. In most C++ implementations the new operator can also be overloaded to define specific behaviors.

http://en.wikipedia.org/wiki/New_(C%2B%2B)

I moved this to a new thread to avoid hijacking the other thread. Continue the discussion about the new operator here if you wish.

Thanks for all your effort, you were absolutely right ...

I got this code from: http://www.cplusplus.com/doc/tutorial/dynamic/

int * bobby;
bobby = new (nothrow) int [5];
if (bobby == 0) {
  // error assigning memory. Take measures.
  };

Is this the right way to do it?

>>Is this the right way to do it?
There are two ways to do it. The preferred way is to use try/catch blocks

int main()
{
    int * bobby;
    try
    {
        bobby = new (nothrow) int [5];
    }
    catch (std::bad_alloc &ba)
    {
        cout << "Oops!  memory allocation failed\n";
        return 1;
    }
    cout << "memory allocation ok\n";
};

The other way (for legacy code) is to use the std::nothrow version

//standard C++ code
#include <new> //required for nothrow new

CWindow p;
p = new(std::nothrow) DerivedWind;
if (!p) //fine now
{
 cout<<"allocation failure!"
 exit(1);
}
//...continue normally

Here is a good link you should read

Are there actually places on the internet where I can find exact 100% correct information about C++ ? (probably Narue's site)

Hi, Ancient Dragon, I read the link you posted, so it's actually not required to use nothrow with a try-catch block or did I get that wrong?

Do I have to precede nothrow with std:: ?

>>>Is this the right way to do it?
>No.
No. When you use nothrow, new returns a null pointer instead of throwing bad_alloc.

Your catch is also malformed. ;)

nothrow is a (ad-hoc) way to control program flow.
As you may be clear, no throw does not throw exceptions.
So you get the old C type check rather than the great exception handling mechanism.

>>>Is this the right way to do it?
>No.
No. When you use nothrow, new returns a null pointer instead of throwing bad_alloc.

Your catch is also malformed. ;)

So the information was correct?

nothrow is a (ad-hoc) way to control program flow.
As you may be clear, no throw does not throw exceptions.
So you get the old C type check rather than the great exception handling mechanism.

Good point :) but is the C++ way of handling exceptions the preferred way to handle this ?

>So the information was correct?
Pretty much.

>but is the C++ way of handling exceptions the preferred way to handle this ?
It depends. Unless you have a good reason though, you should prefer the default throwing behavior.

>but is the C++ way of handling exceptions the preferred way to handle this ?
It depends. Unless you have a good reason though, you should prefer the default throwing behavior.

Is converting C code to C++ code a good reason?

>>Good point but is the C++ way of handling exceptions the preferred way to
>>handle this ?
Yes. It much much better. But then, you can sometime run into situation of using the C-style if checks.
But I generally don't like if-checks. They're ugly. And very ugly.

>>>Is this the right way to do it?
>No.
No. When you use nothrow, new returns a null pointer instead of throwing bad_alloc.

Your catch is also malformed. ;)

Yes I realized that after posting (open mouth and shoved in foot). I probably changed my post since you read it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.