-
February 5th, 2009, 02:57 PM
#1
[RESOLVED] new operator
Hi all,
When operator new fails bad::alloc exception is thrown.
Does the c++ standard guarantee that the pointer will be set to NULL ?
Many thanks
-
February 5th, 2009, 03:02 PM
#2
Re: new operator
No. Anyway, because of the exception, the pointer would be left unchanged.
-
February 6th, 2009, 01:41 AM
#3
Re: new operator
AFAIK, when exception was thrown, stack unwinding is happen and all the local copy get destroyed. Why you said the pointer was unchanged ?
Does it related to new allocate memory on heap ?
Thanks.
Thanks for your help.
-
February 6th, 2009, 01:49 AM
#4
Re: new operator
Originally Posted by Peter_APIIT
AFAIK, when exception was thrown, stack unwinding is happen and all the local copy get destroyed. Why you said the pointer was unchanged ?
If the exception causes control to reach a point where the pointer goes out of scope, then yes, the pointer would be destroyed. However, it is not necessarily the case that the pointer will go out of scope since it could be assigned to in a try block where the exception is caught in the catch block.
-
February 6th, 2009, 02:37 AM
#5
Re: new operator
Sorry for my stupidity.
Do you have any example code that explain your idea ?
Thanks.
Thanks for your help.
-
February 6th, 2009, 02:49 AM
#6
Re: new operator
Originally Posted by Peter_APIIT
Do you have any example code that explain your idea ?
Well, a simple but rather silly example for some type T:
Code:
T* p = 0;
try
{
p = new T;
}
catch (const std::exception&)
{
// do nothing
}
delete p;
My main point is that because the exception is thrown, the assignment does not take place, so the pointer retains its value. If the pointer happens to be destroyed immediately after that, so be it.
-
February 6th, 2009, 03:55 AM
#7
Re: new operator
Thanks you!
EDIT: How do I mark the message as a resolved?? Thank you
-
February 6th, 2009, 04:06 AM
#8
Re: new operator
You're welcome
You can use the thread tools to mark the thread as resolved.
-
February 6th, 2009, 05:54 AM
#9
Re: [RESOLVED] new operator
Just a complement...
Still, it's possible to use new in the "old" form. This is done with the nothrow version of new and is guaranteed by the Standard to return 0 in case of failure.
Code:
SomeType p = new (std::nothrow) SomeType;
if (!p)
{
//Failure...
}
-
February 7th, 2009, 07:15 AM
#10
Re: [RESOLVED] new operator
Originally Posted by ltcmelo
Just a complement...
Still, it's possible to use new in the "old" form. This is done with the nothrow version of new and is guaranteed by the Standard to return 0 in case of failure.
Code:
SomeType p = new (std::nothrow) SomeType;
if (!p)
{
//Failure...
}
Watch out from std::nothrow() when there is a chance that your code may be used in environments that define thier own version of new operayor (MFC for example).
http://www.codeguru.com/forum/showthread.php?t=470168
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|