473,231 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 3769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
6
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to...
10
by: Alex Vinokur | last post by:
GNU g++ 3.3.3, Cygwin // Stuff static char* mbuffer = NULL; // Stuff void doit()
3
by: Roger Davis | last post by:
I am trying to use nothrow new() and am encountering what seems to be a bug in the SGI/Irix C++ environment. The following program runs OK under Solaris and Linux, but dumps core at the delete...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
4
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1);...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
1
by: Lighter | last post by:
Is there a way to write a memory leak detector supporting new(nothrow)? For example, #include <My_Debug_New.h> using namespace std; int main() {
4
by: Alex Vinokur | last post by:
Foo* p = new (nothrow) Foo; Should we use operator delete (p, nothrow) ? Or can we use delete p; ? If we use operator delete (p, nothrow) how to replace it by operator style?
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.