473,808 Members | 2,761 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
19 2695

"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
news:3f******** *************@r ead.news.gr.uu. net...
| "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 ?".

Yes, I know that.

| That means that the pointer will not point to 0.

That is why I said:

*If you set the pointer to zero *straight after* you have deleted it*

Meaning, the first time you delete it, set it to zero, making the
behaviour of deleting it again defined and safe.

Cheers.
Chris Val
Jul 19 '05 #11
On Sun, 13 Jul 2003 02:21:37 +1000, "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote:

"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
news:3f******* **************@ read.news.gr.uu .net...
| "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 ?".

Yes, I know that.

| That means that the pointer will not point to 0.

That is why I said:

*If you set the pointer to zero *straight after* you have deleted it*

Meaning, the first time you delete it, set it to zero, making the
behaviour of deleting it again defined and safe.


It's possibly not meaningful to discuss the finer points of language
here, but at least regarding the last few postings there seems to be
confusion about the concept of deleting an object (which cannot be
done without a non-null pointer value) and "deleting a pointer",
probably meaning to use that pointer in a delete expression, which if
it has a technical meaning would be a totally _different_ meaning.

Since the original poster wrote about deleting objects, not pointers,
and furthermore about deleting the same object twice, the only
reasonable interpretation is that what was meant was first deleting
the object, and then performing a delete operation again on the now
invalid pointer value (which for this technically can't be zero).

Setting a pointer to zero after the object it denoted has been
deleted is not an evil practice, though, but don't rely on it to catch
all errors associated with deallocation -- some argue that it might
give a false sense of security + complicates the code + may hide some
errors/bugs, and therefore should be _avoided_ one hundred percent.

Jul 19 '05 #12

"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******** ********@News.C IS.DFN.DE...
| On Sun, 13 Jul 2003 02:21:37 +1000, "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote:
|
| >
| >"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
| >news:3f******* **************@ read.news.gr.uu .net...
| >| "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 ?".
| >
| >Yes, I know that.
| >
| >| That means that the pointer will not point to 0.
| >
| >That is why I said:
| >
| >*If you set the pointer to zero *straight after* you have deleted it*
| >
| >Meaning, the first time you delete it, set it to zero, making the
| >behaviour of deleting it again defined and safe.
|
| It's possibly not meaningful to discuss the finer points of language
| here, but at least regarding the last few postings there seems to be
| confusion about the concept of deleting an object (which cannot be
| done without a non-null pointer value) and "deleting a pointer",
| probably meaning to use that pointer in a delete expression, which if
| it has a technical meaning would be a totally _different_ meaning.

Fair enough.

| Since the original poster wrote about deleting objects, not pointers,
| and furthermore about deleting the same object twice, the only
| reasonable interpretation is that what was meant was first deleting
| the object, and then performing a delete operation again on the now
| invalid pointer value (which for this technically can't be zero).

I understand the context in which the OP posted his/her question.

I only provided an suggestion. That is why I used words such as:
'depends', 'if' and 'after'.

| Setting a pointer to zero after the object it denoted has been
| deleted is not an evil practice, though, but don't rely on it to catch
| all errors associated with deallocation -- some argue that it might
| give a false sense of security + complicates the code + may hide some
| errors/bugs, and therefore should be _avoided_ one hundred percent.

I've never heard anyone suggest that before.
Can you give me an example of where such circumstances can arise ?.

Cheers.
Chris Val
Jul 19 '05 #13
On Sun, 13 Jul 2003 03:29:50 +1000, "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******* *********@News. CIS.DFN.DE...
| Setting a pointer to zero after the object it denoted has been
| deleted is not an evil practice, though, but don't rely on it to catch
| all errors associated with deallocation -- some argue that it might
| give a false sense of security + complicates the code + may hide some
| errors/bugs, and therefore should be _avoided_ one hundred percent.

I've never heard anyone suggest that before.
Can you give me an example of where such circumstances can arise ?.


Not that this is my argument, I think the pros and cons nearly cancel
out, but here is one scenario. A firm or shop or project standardizes
on setting pointers to zero after delete. Such pointers can safely be
passed to functions that check for null, and so they're passed around.
In one function there is an incorrect delete via the passed pointer.
This isn't detected because the pointer is null. Later that function
is used with a more valuable real pointer value. It then incorrectly
deletes an object it shouldn't. The effect only shows up much later
in the code execution, and it's then very hard to track down the cause.
Jul 19 '05 #14
"Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
news:be******** ****@ID-110726.news.uni-berlin.de...


I've never heard anyone suggest that before.
Can you give me an example of where such circumstances can arise ?.


There are many philosophical approaches. Many believe -including me- that
the second deletion of an object is the last thing of improper use one will
do on it, before he will have tried to read or write to it.

A better use is always use the standard library containers instead of
allocating and deallocating memory explicitly. So for example, instead of
using an array of pointers, use a vector of objects or whatever container of
the standard library fits your purpose better (e.g. a list).



--
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 #15

"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
news:3f******** *************@r ead.news.gr.uu. net...
| "Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
| news:be******** ****@ID-110726.news.uni-berlin.de...
| >
| >
| > I've never heard anyone suggest that before.
| > Can you give me an example of where such circumstances can arise ?.
|
|
|
| There are many philosophical approaches. Many believe -including me- that
| the second deletion of an object is the last thing of improper use one will
| do on it, before he will have tried to read or write to it.

Point taken.

| A better use is always use the standard library containers instead of
| allocating and deallocating memory explicitly. So for example, instead of
| using an array of pointers, use a vector of objects or whatever container of
| the standard library fits your purpose better (e.g. a list).

Agreed.

Cheers.
Chris Val
Jul 19 '05 #16

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

How can you point two pointers (allocated using new) to the same location???
If you are pointing a pointer to an already allocated piece of memory, you
CANT use the "reference-counting kind of thing in constructor".

The best solution, IMHO, would be to use smart pointers.
--
-Ashish
--------------------------------------------------------------------------
Hi! I'm a shareware signature! Send $5 if you use me, send $10 for manual!
http://www.123ashish.com http://www.softwarefreaks.com

Jul 19 '05 #17

"Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
news:be******** ****@ID-110726.news.uni-berlin.de...

"Ioannis Vranos" <iv*@nothis.ema ils.ru> wrote in message
news:3f******** *************@r ead.news.gr.uu. net...
| "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 ?".

Yes, I know that.

| That means that the pointer will not point to 0.

That is why I said:

*If you set the pointer to zero *straight after* you have deleted it*
I think you have misunderstood the problem.
The OP's problem is that more than one pointers point to the same location.

Lets assume that p1 and p2 point to location m1.
Delete m1 using p1.
Set p1 to NULL.
So far so good.
Now, delete p2, the behaviour is undefined. And you had no idea that p2 also
points to m1.


Meaning, the first time you delete it, set it to zero, making the
behaviour of deleting it again defined and safe.

Cheers.
Chris Val

--
-Ashish
--------------------------------------------------------------------------
Hi! I'm a shareware signature! Send $5 if you use me, send $10 for manual!
http://www.123ashish.com http://www.softwarefreaks.com
Jul 19 '05 #18


"Chris ( Val )" wrote:

| I think you have misunderstood the problem.
| The OP's problem is that more than one pointers point to the same location.
|
| Lets assume that p1 and p2 point to location m1.
| Delete m1 using p1.
| Set p1 to NULL.
| So far so good.
| Now, delete p2, the behaviour is undefined. And you had no idea that p2 also
| points to m1.

Yes, that is true.

However, its the programmers job and responsibility to keep
track of such things, and take preventative measures to avoid
this issue all together.


Exactly.

But especially newbies tend to think: If I set the pointer to
0 immediatly after the delete, nothing bad can happen any more.
This (having a second pointer to the same object) is a counter-
example. That's why lots of programmers warn about setting
a pointer to 0: It creates a false sense of security.
Don't get me wrong: setting a pointer to 0 can't do any harm
and is often a good idea. But it doesn't solve all dynamic
memory related problems.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #19

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:3F******** *******@gascad. at...
|
|
| "Chris ( Val )" wrote:
| >
| > | I think you have misunderstood the problem.
| > | The OP's problem is that more than one pointers point to the same location.
| > |
| > | Lets assume that p1 and p2 point to location m1.
| > | Delete m1 using p1.
| > | Set p1 to NULL.
| > | So far so good.
| > | Now, delete p2, the behaviour is undefined. And you had no idea that p2 also
| > | points to m1.
| >
| > Yes, that is true.
| >
| > However, its the programmers job and responsibility to keep
| > track of such things, and take preventative measures to avoid
| > this issue all together.
| >
|
| Exactly.
|
| But especially newbies tend to think: If I set the pointer to
| 0 immediatly after the delete, nothing bad can happen any more.
| This (having a second pointer to the same object) is a counter-
| example. That's why lots of programmers warn about setting
| a pointer to 0: It creates a false sense of security.
| Don't get me wrong: setting a pointer to 0 can't do any harm
| and is often a good idea. But it doesn't solve all dynamic
| memory related problems.

Point taken :-).

Thanks Karl.
Chris Val
Jul 19 '05 #20

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
3851
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
9422
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
3889
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
4660
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
8182
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
5034
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
2277
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
9721
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
10631
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
10374
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
7651
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
5548
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
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.