how to test whether auto pointer is null?
Asked Answered
P

5

10

I'm new to auto pointer. I have this:

std::auto_ptr<myClass> myPointer(new MyClass(someArg));

How do I test whether I can instantiate myPointer successfully? I tried if (myPointer==NULL) and the compiler emitted an error:

no operator "==" matches these operands.

Prowess answered 15/8, 2011 at 20:3 Comment(5)
Always try to reproduce error messages verbatim in their entirety. The compiler normally will have dropped all unimportant bits before you get to see them.Labiodental
Seriously, though, what do you mean by "instantiate" here?Fremd
I think whether I can instantiate myPointer successfully means just whether myPointer was created successfully. The question and its answers - most of all the one of Lightness Races in Orbit - could be even better without this distraction.Mcguire
Remember Occam's Razor guys (and gals). Code posted is often trimmed to reduce verbosity, and it ain't necessarily so that the allocation of myPointer is immediately followed by the NULL/nullptr test.Detritus
@brewmanz: If the OP "trimmed" the code so much that it doesn't actually demonstrate the problem any more, then that's the opposite of Occam's Razor. It's also entirely useless as a question.Fremd
F
23

What do you mean by "instantiate"?

On a standard-compliant implementation, either the construction of the MyClass succeeded, or an exception was thrown and the auto_ptr will no longer be in scope. So, in the example you provided, the value of the pointer represented by your auto_ptr cannot be NULL.

(It is possible that you are using an implementation without exception support, that can return NULL on allocation failure (instead of throwing an exception), even without the use of the (nothrow) specifier, but this is not the general case.)


Speaking generally, you can check the pointer's value. You just have to get at the underlying representation because, as you've discovered, std::auto_ptr does not have an operator==.

To do this, use X* std::auto_ptr<X>::get() const throw(), like this:

if (myPointer.get()) {
   // ...
}

Also note that std::auto_ptr is deprecated in C++0x, in favour of std::unique_ptr. Prefer the latter where you have access to a conforming implementation.

Fremd answered 15/8, 2011 at 20:5 Comment(10)
The OP might be working in a context where exceptions are not used (so new can return NULL). This is far more common than people like to admit.Huddle
@Zack: Is that valid without specifying (nothrow)? @Zack: Seems that's only relevant for non-compliant compilers, and I'm not bothered about them. This question is about C++.Fremd
@Tomalak Geret'kal On embedded compilers that do not support exceptions there's no need to call the std::nothrow version of new. Either call will return NULL if the allocation fails.Renal
@Praetorian: Hmm, OK I'll edit that in; it's non-compliant though.Fremd
Per your answer - "Either the construction of the MyClass succeeded, or an exception was thrown and the auto_ptr will no longer be in scope." Does auto_pointer automatically throws an exception eventhough I dont have throw command in MyClass's constructor?Prowess
@user853069: No, but the allocator behind new will throw std::bad_alloc for you if the memory couldn't be allocated.Fremd
Even in non-embedded environments, exception handling is disabled for a lot of real, production code (for instance, I can personally attest that both Firefox and Webkit do this, and I've heard that it is also the case for OpenOffice). Yes, this is a non-compliant mode for the compiler. Yes, it means much of the standard library is unusable or does not behave as documented. It's still C++ as she is spoke, and I think it behooves us (answerers of StackOverflow questions) to be aware of it.Huddle
I guess if I still want to use auto pointer, I have to do something like this right? try{ std::auto_ptr<myClass> myPointer(new MyClass(someArg));Prowess
@user853069: Only if you want to handle the std::bad_alloc. Usually you don't bother: if you've run out of memory, there's not a lot of point in continuing to execute the program. Just let it bubble up to main and terminate your process. (The use of std::auto_ptr is not related to this aspect of dynamically-allocating objects.)Fremd
N.B. it'll only bubble up to main if you have a catch in main!Fremd
V
5

How about this?

 if(myPointer.get()==NULL)
Valency answered 15/8, 2011 at 20:6 Comment(0)
A
4

I think myPointer.get() == NULL is what you're looking for.

Aloe answered 15/8, 2011 at 20:6 Comment(0)
P
1

Given the statement you wrote in your question either myPointer is set or an exception was thrown and catching the exception is the correct way to check if anything went wrong.

In any case you can get the underlying pointer by calling auto_ptr::get().

Pulpy answered 15/8, 2011 at 20:7 Comment(0)
M
1

When checking an std::auto_ptr for NULL, I think this is most idiomatic notation:

if (!myPointer.get()) {
    // do not dereference here
}
Mcguire answered 19/5, 2014 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.