473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3804
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_EVALUATI ON_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_EVALUATI ON_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
1861
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 change them.) We can replace operator new functionality by providing additional parameters to the new function call (ala Stroustrup): class foo_t;
6
5885
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 work fine on its own but when I tried to use say 'cout' or 'cerr' from STL the tester crashed upon termination. using some breakpoints I found out that when I make use of 'cerr' in the program, there is a call to my override of delete which do not...
10
3500
by: Alex Vinokur | last post by:
GNU g++ 3.3.3, Cygwin // Stuff static char* mbuffer = NULL; // Stuff void doit()
3
2472
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 statement under Irix. Is this really an SGI implementation bug, or have I done something stupid? (Other than trying to use nothrow new(), that is ;-) ) #include <new> #include <stdexcept>
2
2081
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 operator, placement, nothrow, arrays, etc... My books cover the topic, I've found FAQs on the web that cover the topic, and so on, but all the sources I've found are disjointed. There's a bit on this page, a bit on that page, and so on. The...
4
4860
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); delete p2; ------------------------------------------ Alex Vinokur
6
12456
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 delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
1
2067
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
6517
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
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.