now how do i validate pt before using pt.get()i.e
how can I make sure that pt has been allocated....
is there any way to validate, as iam using this for the first time.Kindly someone clarify
Thanx in advance
May 2nd, 2004, 02:54 AM
Andreas Masur
Actually, you already answered your own question....'get' will return 0 in case of a failure. Besides that...'new' is supposed to throw a 'bad_alloc' exception in case of a failure...
May 2nd, 2004, 07:26 PM
kasracer
Adding onto his question, do you know of any lists that basically say what can throw what excepts and when it happens? It would be nice to trap specific errors for debugging.
May 2nd, 2004, 11:49 PM
kvpreeth
Thanx...
but I still have a doubt, i.e
in case of normal allocation using new
for eg
a = new b;
we can check
if( a!= NULL)
{}
similarly without using get can we check the same in the case of auto_ptr
May 3rd, 2004, 06:57 AM
Assmaster
Firstly... Like Andreas said... new should not return NULL according to the standard. If allocation fails, it should throw a std::bad_alloc exception, and therefore you should not need to check for NULL. (VC++ does however return NULL if allocation fails, because it is not conforming to the standard at that point. If that's your compiler, the check is valid.)
Secondly... Why can't you use get() to check the validity?
Code:
if(!your_auto_ptr.get())
{
// something went wrong!
}
May 3rd, 2004, 04:37 PM
Graham
Just as a quick point in partial answer to one question raised earlier about what exceptions can be thrown: std::auto_ptr has a nothrow guarantee on all its member functions.