473,396 Members | 1,757 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,396 software developers and data experts.

delete and exception

Hi all,
suppose the ctor of the class throws. Thus, at this point in time, some
memory has been allocated but the object couldnot be constructed. Also,
the memory is not yet freed. Does the c++ std automatically guarantee
the call to operator delete?

The relavent code is :

class Int {
public:
Int()
{
throw 100;
}
~Int()
{
// this will never be called, since the ctor throws.
}
};
int main()
{
Int *p = new Int;

// will p be deleted at this point without doing an explicit call to
delete (provided the ctor throws)?
}

TC++PL Edition 3, 14.4.5 says that the memory should be freed. But it
doesnot say clearly whether it will be freed by calling operator
delete() or something else.
Thanks in advance
Neelesh,

Nov 22 '05 #1
10 2556
"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
: Hi all,
: suppose the ctor of the class throws. Thus, at this point in time, some
: memory has been allocated but the object couldnot be constructed. Also,
: the memory is not yet freed. Does the c++ std automatically guarantee
: the call to operator delete?
Yes.

: TC++PL Edition 3, 14.4.5 says that the memory should be freed. But it
: doesnot say clearly whether it will be freed by calling operator
: delete() or something else.

Each new operator has a corresponding delete-operator, which is called
automatically when the construction of an allocated object fails.

In the standard, this is described in §5.3.4/17:
<<< If any part of the object initialization described above terminates by
throwing an exception and a suitable deallocation function can be found,
the deallocation function is called to free the memory in which the object
was being constructed, after which the exception continues to propagate in
the context of the new-expression. If no unambiguous matching
deallocation function can be found, propagating the exception does not
cause the object's memory to be freed. [Note: This is appropriate when
the called allocation function does not allocate memory; otherwise, it is
likely to result in a memory leak. ] >>>

This is actually the (only) purpose of placement-delete operators:
When defining a placement-new operator on your own, you must also
provide a matching placement-delete operator (with the same extra
parameters), which will be called to released allocated memory
in case a constructor throws.

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 22 '05 #2

Thanks Ivan.

So suppose I write my own operator new and operator delete to the
previous code. (Note - they are not placement operators)

#include <iostream>
using namespace std;

void * operator new (size_t t) // my own global operator new
{
cout << "operator new called" << endl;
return malloc(t);
}

void operator delete(void* mem) // corresponding operator delete
{
cout << "operator delete called" << endl;
free(mem);
}

// Rest of the code is same as given in the previous post, copied here
for completeness.

class Int {
public:
Int() { cout << "Constructor called " << endl; throw 100; }
~Int() { cout << "destructor called" << endl; }
};

int main()
{
Int *p = new Int;
}
In this case, I observe that the messages "operator new called" and
"Constructor called".Also, the ctor throws. But I donot see the message
"operator delete called" printed anywhere.
Should'nt the memory allocated by ::operator new() be freed using
::operator delete() ?

I am using g++ 3.4.2.

What am I exactly missing here?

Thanks a lot,
Neelesh

Nov 22 '05 #3

"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

Thanks Ivan.

So suppose I write my own operator new and operator delete to the
previous code. (Note - they are not placement operators)

#include <iostream>
using namespace std;

void * operator new (size_t t) // my own global operator new
{
cout << "operator new called" << endl;
return malloc(t);
}

void operator delete(void* mem) // corresponding operator delete
{
cout << "operator delete called" << endl;
free(mem);
}

// Rest of the code is same as given in the previous post, copied here
for completeness.

class Int {
public:
Int() { cout << "Constructor called " << endl; throw 100; }
~Int() { cout << "destructor called" << endl; }
};

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

int main()
{
try {
Int *p = new Int;
}
catch(...) {
std::cerr << "\n\nCaught\n\n";;
}
}

In this case, I observe that the messages "operator new called" and
"Constructor called".Also, the ctor throws. But I donot see the message
"operator delete called" printed anywhere.
Should'nt the memory allocated by ::operator new() be freed using
::operator delete() ?

I am using g++ 3.4.2.

What am I exactly missing here?


IIRC, if you do not catch an exception, terminate() will be called. The
default behavior of terminate() is to call abort().

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Nov 22 '05 #4

Sumit Rajan wrote:
IIRC, if you do not catch an exception, terminate() will be called. The
default behavior of terminate() is to call abort().


Yes Sumit, you are right. I forgot this subtle point that stack
unwinding takes place _only_ when the exception is caught, not
otherwise.

Thanks a lot.

Nov 22 '05 #5
"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
:
: Sumit Rajan wrote:
: > IIRC, if you do not catch an exception, terminate() will be called.
The
: > default behavior of terminate() is to call abort().
: >
:
: Yes Sumit, you are right. I forgot this subtle point that stack
: unwinding takes place _only_ when the exception is caught, not
: otherwise.

Actually, I think this is implementation-defined: when an exception
is not caught, unwinding may or may not take place.

In your case and on your platform, it obviously did not take place.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


Nov 22 '05 #6

Ivan Vecerina wrote:
Actually, I think this is implementation-defined: when an exception
is not caught, unwinding may or may not take place.


Oh, thanks for that. I was probably under a wrong assumption. So what I
now understand is that - stack unwinding is _guaranteed to take place_
only if an exception is caught.

Thanks.

Nov 22 '05 #7
In article <11**********************@g43g2000cwa.googlegroups .com>,
Neelesh Bodas <ne***********@gmail.com> wrote:

Sumit Rajan wrote:
IIRC, if you do not catch an exception, terminate() will be called. The
default behavior of terminate() is to call abort().


Yes Sumit, you are right. I forgot this subtle point that stack
unwinding takes place _only_ when the exception is caught, not
otherwise.


If I understand what you just meant, no, that's not the case.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #8

Greg Comeau wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
Neelesh Bodas <ne***********@gmail.com> wrote:


Yes Sumit, you are right. I forgot this subtle point that stack
unwinding takes place _only_ when the exception is caught, not
otherwise.


If I understand what you just meant, no, that's not the case.


So do you mean to say (please select exactly one option ;-) )
1) Stack unwinding might or might not take place when exception goes
uncaught
2) Stack unwinding _must not_ take place when the exception is
uncaught?
3) Stack unwinding _must_ take place whenever an exception is thrown,
irrespective of whether exception is caught or uncaught
4) None of the above. (please give explanation if you select this)

actually I am almost totally confused. Thanks for the help.

Nov 22 '05 #9
In article <11**********************@f14g2000cwb.googlegroups .com>,
Neelesh Bodas <ne***********@gmail.com> wrote:
Greg Comeau wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
Neelesh Bodas <ne***********@gmail.com> wrote:
>Yes Sumit, you are right. I forgot this subtle point that stack
>unwinding takes place _only_ when the exception is caught, not
>otherwise.


If I understand what you just meant, no, that's not the case.


So do you mean to say (please select exactly one option ;-) )
1) Stack unwinding might or might not take place when exception goes
uncaught
2) Stack unwinding _must not_ take place when the exception is
uncaught?
3) Stack unwinding _must_ take place whenever an exception is thrown,
irrespective of whether exception is caught or uncaught
4) None of the above. (please give explanation if you select this)

actually I am almost totally confused. Thanks for the help.


15.5.1p2: "In the situation where no matching handler is found,
it is implementation-defined whether or not the stack is unwound
before terminate()is called. In all other situations, the stack
shall not be unwound before terminate()is called. An implementation
is not permitted to finish stack unwinding prematurely based on a
determination that the unwind process will eventually cause a call
to terminate()."
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #10

Greg Comeau wrote:
15.5.1p2: "In the situation where no matching handler is found,
it is implementation-defined whether or not the stack is unwound
before terminate()is called. In all other situations, the stack
shall not be unwound before terminate()is called. An implementation
is not permitted to finish stack unwinding prematurely based on a
determination that the unwind process will eventually cause a call
to terminate()."


Yes, the confusion exists no more. Thanks.

Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
0
by: Kevin | last post by:
I am successfully creating and deleting appointments and sending invitations in a C# / ASP.NET / Exchange2000 environment. It works great! But the problem is that no notifications are sent out...
9
by: learning | last post by:
hi I am trying to make some simple app to learn exception safety coding. I know that new and delete can throw bad_alloc but how can I force them to throw by a flag "change" at run time? I am...
9
by: Alex Vinokur | last post by:
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
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
5
by: siddhu | last post by:
What happens when deletion falis?Does operator delete function throw exception? I assume that nothrow is not available with delete. Or is it available?
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
2
by: mathewgk80 | last post by:
Hi all, i got an error "System.NotSupportedException: Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified" when i try to delete a row.. the code is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.