By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

delete(nothrow)

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
Alex Vinokur wrote:
What is difference between
delete p;
and
delete(nothrow)p;
?
Is "nothrow" a typedef ?
Sep 24 '06 #2

P: n/a
just google for:

nothrow c++
Sep 24 '06 #3

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.