473,799 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete on delete !

Hi,

If I do another delete on an object which has been deleted earlier, then
how is
the system expected to behave ?

Is it an unpredictable behavior ??

Thanks
Sandeep

Jul 19 '05 #1
19 2694
Sandeep Grover wrote:
Hi,

If I do another delete on an object which has been deleted earlier, then
how is
the system expected to behave ?

Is it an unpredictable behavior ??


Yes, unpredictable.

Some will choke, some destructors SEGV, it goes on and on.

Jul 19 '05 #2
Sandeep Grover wrote:
If I do another delete on an object which has been deleted earlier, then
how is the system expected to behave?

Is it an unpredictable behavior ??


It's undefined, so in theory, yes; anything could happen.

S

Jul 19 '05 #3

Sandeep Grover wrote:
If I do another delete on an object which has been deleted earlier, then
how is the system expected to behave?

Is it an unpredictable behavior ??


It's undefined, so in theory, yes; anything could happen.

S


Thanks !

So, if I have an array of pointers; more than one of the entries could point
to
the same chunk of memory (allocated using new). How do I ensure that I
end up deleting that entry only once. I dont want to use reference-counting
kind of thing in constructor.

Jul 19 '05 #4
On Fri, 11 Jul 2003 16:53:31 -0700, Sandeep Grover <sa*****@magm a-da.com> wrote:
[i]f I have an array of pointers; more than one of the entries could
point to the same chunk of memory (allocated using new). How do I ensure
that I end up deleting that entry only once. I dont want to use
reference-counting kind of thing in constructor.


The sane thing to do would be to use reference-counting.

Reference-counting is automated by many smart-pointers; the most general
and compatible with upcoming standard is boost::shared_p tr (go to
<url:hhtp://www.boost.org> to download the Boost library, it's free).

Without reference-counting you can just sort the array first, on the
pointer values; even though pointers that do not point into the same
variable are not formally comparable, they are comparable in practice.
But you need to know that the array contains pointers to all objects
that you need to delete. And in that case, why not store the unique
pointers in a separate array of the same size or less?

Jul 19 '05 #5
"Sandeep Grover" <sa*****@magm a-da.com> wrote in message
news:3F******** *******@magma-da.com...

Sandeep Grover wrote:
If I do another delete on an object which has been deleted earlier, then how is the system expected to behave?

Is it an unpredictable behavior ??
It's undefined, so in theory, yes; anything could happen.

S


Thanks !

So, if I have an array of pointers; more than one of the entries could

point to
the same chunk of memory (allocated using new). How do I ensure that I
end up deleting that entry only once. I dont want to use reference-counting kind of thing in constructor.

Use a vector of the objects you want, or a list if it suits better the
problem, and not a built in array of pointers.



--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

Jul 19 '05 #6
"Alf P. Steinbach" wrote:

Without reference-counting you can just sort the array first, on the
pointer values; even though pointers that do not point into the same
variable are not formally comparable, they are comparable in practice.


If you use std::greater, std::less, std::greater_eq ual, or
std::less_equal they are comparable. It's just the builtin operators
that are allowed to be fast. <g>

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #7
Sandeep Grover wrote:
Sandeep Grover wrote:

If I do another delete on an object which has been deleted earlier, then
how is the system expected to behave?

Is it an unpredictable behavior ??


It's undefined, so in theory, yes; anything could happen.

S

Thanks !

So, if I have an array of pointers; more than one of the entries could point
to the same chunk of memory (allocated using new). How do I ensure that I
end up deleting that entry only once. I dont want to use reference-counting
kind of thing in constructor.


A flippant answer would be not to alias pointers.

On the other hand, techniques such as reference-counting
are for when you need multiple pointers to the same object
which you wish to dispose of independently, but don't need
an invasive implementation, i.e. an implementation inside
the pointed-to object.

But this is really a design issue, not a language issue.

S

Jul 19 '05 #8

"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
news:3f******** *************@r ead.news.gr.uu. net...
| "Sandeep Grover" <sa*****@magm a-da.com> wrote in message
| news:3F******** *******@magma-da.com...
| > Hi,
| >
| > If I do another delete on an object which has been deleted earlier, then
| > how is
| > the system expected to behave ?
|
|
| It will get angry.

Depends.

| > Is it an unpredictable behavior ??

| Yes it is undefined behaviour. It may die, it may continue living and die
| later, it may truncate data, everything.

Again it depends.

If you set the pointer to zero straight after you have deleted
it, then the behaviour is very defined, and safe.

For example:

# include <iostream>
# include <ostream>

struct Base
{
int N;
};

int main()
{
Base* B = new Base;

delete B;
B = 0; // Introduce a safety net.

for( int Idx( 0 ); Idx < 5; ++Idx )
delete B; // Safe - no opp.

return 0;
}

Cheers.
Chris Val
Jul 19 '05 #9
"Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
news:be******** ****@ID-110726.news.uni-berlin.de...


| Yes it is undefined behaviour. It may die, it may continue living and die | later, it may truncate data, everything.

Again it depends.

If you set the pointer to zero straight after you have deleted
it, then the behaviour is very defined, and safe.

He/she said:

"If I do another delete on an object which has been deleted earlier, then
how is the system expected to behave ?".
That means that the pointer will not point to 0.


--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Jul 19 '05 #10

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

Similar topics

2
2599
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown during construction, a form of
1
3849
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
3
9418
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
1
3888
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
2093
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...
3
4656
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
9
8179
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); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
10
2097
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
15
5027
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 - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
29
2276
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can learn and refresh my memory? :-)
0
9540
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
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7564
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
6805
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.