468,512 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

delete(nothrow)

What is difference between
delete p;
and
delete(nothrow)p;
?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Sep 24 '06 #1
9 3363
Alex Vinokur wrote:
What is difference between
delete p;
and
delete(nothrow)p;
?
Is "nothrow" a typedef ?
Sep 24 '06 #2
just google for:

nothrow c++
Sep 24 '06 #3
Alex Vinokur wrote:
What is difference between
delete p;
and
delete(nothrow)p;
?
Well, let's run an experiment and see:

int main()
{
int* p = new int;
delete p;
}

Compiling with Comeau Test Drive ...

Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !

Your Comeau C/C++ test results are as follows:

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for
ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing. All rights reserved.
MODE:strict errors C++

In strict mode, with -tused, Compile succeeded (but remember,
the Comeau online
compiler does not link)."

OK, so that seemed to work fine.

Now let's try this:

int main()
{
int* p = new int;
delete (nothrow) p;
}

Compiling again with Comeau Test Drive...

Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !

Your Comeau C/C++ test results are as follows:

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for
ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 4: error: identifier "nothrow" is undefined
delete (nothrow) p;
^

"ComeauTest.c", line 4: error: expected a ";"
(perhaps on the previous statement)
delete (nothrow) p;
^

2 errors detected in the compilation of "ComeauTest.c".

In strict mode, with -tused, Compile failed
Hit the Back Button to review your code and compile options.

So it appears that the first is legal C++ and the second is a syntax
error.

Best regards,

Tom

Sep 24 '06 #4
hello Alex,
when you assign memory dynamically theres a chance of failure of
allocation of requested memory size especially with arrays. When new
fails to allocate the requested memory it throws a bad_alloc exception
of base class exception. Some compilers version returns a 0 instead for
bad memory allocation. Specifiying (nothrow)
such as
double *ptr = new(nothrow) double[5000000];
requests the compiler to assign a 0 incase of bad allocation instead of
throwing an exception of bad_alloc.

Similarly the same rule follows with delete,
when you delete a pointer and if it fails it will throw automatically
an exception.
the no throw requests it to not throw any exception.
Hopee this answer your question
Saad

Alex Vinokur wrote:
What is difference between
delete p;
and
delete(nothrow)p;
?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Sep 24 '06 #5
Thomas Tutone wrote:
>
So it appears that the first is legal C++ and the second is a syntax
error.
That's because you wrote the test case incorrectly. In fact, the second
is valid, and calls the nothrow version of operator new. Fix your test
case by adding #include <new>.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #6
Pete Becker wrote:
Thomas Tutone wrote:
>>
So it appears that the first is legal C++ and the second is a syntax
error.

That's because you wrote the test case incorrectly. In fact, the second
is valid, and calls the nothrow version of operator new. Fix your test
case by adding #include <new>.
Whoops, never mind. <gI thought the question was about new(nothrow).

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #7
Alex Vinokur wrote :
What is difference between
delete p;
and
delete(nothrow)p;
?
delete(nothrow) does not exist, since delete doesn't throw.
It exists for new though.
Sep 24 '06 #8
netsecure posted:
Similarly the same rule follows with delete,
when you delete a pointer and if it fails it will throw automatically
an exception.

Why would "delete" throw an exception... what purpose would it serve?
Furthermore, why would it fail? We all know that the following invokes UB
rather than failure:

int i;
delete &i;

--

Frederick Gotham
Sep 24 '06 #9
netsecure wrote:
Alex Vinokur wrote:
>What is difference between
delete p;
and
delete(nothrow)p;
?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Top-posting corrected.
hello Alex,
when you assign memory dynamically theres a chance of failure of
allocation of requested memory size especially with arrays. When new
fails to allocate the requested memory it throws a bad_alloc exception
of base class exception. Some compilers version returns a 0 instead for
bad memory allocation.
Only ancient/non-Standard compliant compilers do so. The standard
library new must throw std::bad_alloc on failure to allocate.

Specifiying (nothrow)
such as
double *ptr = new(nothrow) double[5000000];
requests the compiler to assign a 0 incase of bad allocation instead of
throwing an exception of bad_alloc.
This is correct

As for delete(nothrow), there is no such animal. See 18.4.

Sep 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jay Nabonne | last post: by
6 posts views Thread by shoosh | last post: by
10 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by Roger Davis | last post: by
2 posts views Thread by Dave | last post: by
4 posts views Thread by Alex Vinokur | last post: by
6 posts views Thread by Lighter | last post: by
4 posts views Thread by Alex Vinokur | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.