delete(nothrow)

T

Thomas Tutone

Alex said:
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
 
N

netsecure

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
 
P

Pete Becker

Thomas said:
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.
 
P

Pete Becker

Pete said:
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. <g> I 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.
 
L

loufoque

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.
 
F

Frederick Gotham

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;
 
R

red floyd

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.
 
Joined
Jan 2, 2009
Messages
1
Reaction score
0
delete (nothrow)

Hi all,

From where I stand I see that "
::eek:perator delete(pointer, nothrow)
" is a valid C++ statement. I am sure it is part of the standard for C++0x and I don't _guess_ it is a new feature in the C++0x standard library (my compiler MSVC 9.0 compiles it fine).

The only thing about this operator is to know how to invoke it, a little tricky I admit.

::eek:perator delete (pointer, nothrow);

where pointer is what needs to be deleted.

With best regards
Me
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top