Why does "delete" not delete?

D

D. Susman

Hi,

When I call "clean" as shown below, shouldn't I be getting
segmentation fault due to garbage data? I am compiling with CC and the
output is 5. Does "delete" not delete in this situation or is it no
guarantee that it will print 5 or go wrong?

void clean( int* ptr )
{
delete ptr;
}

//...

int* ptr = new int( 5 );

clean( ptr );

cout << *ptr << endl;
 
A

alasham.said

Hi,

When I call "clean" as shown below, shouldn't I be getting
segmentation fault due to garbage data? I am compiling with CC and the
output is 5. Does "delete" not delete in this situation or is it no
guarantee that it will print 5 or go wrong?

void clean( int* ptr )
{
delete ptr;

}

//...

int* ptr = new int( 5 );

clean( ptr );

cout << *ptr << endl;

It is 'undefined behaviour', so the result may vary from one
implementation to another, and on different runs of the same program.

Regards.
 
D

D. Susman

It is 'undefined behaviour', so the result may vary from one
implementation to another, and on different runs of the same program.

Regards.

Thank you. Is there a way to make a pointer undeletable when passed to
a function?
 
D

dizzy

D. Susman said:
Thank you. Is there a way to make a pointer undeletable when passed to
a function?

Im not sure what you mean by that, but if you make it 0 (the null pointer)
then delete won't do anything with it (will ignore).
 
D

D. Susman

Im not sure what you mean by that, but if you make it 0 (the null pointer)
then delete won't do anything with it (will ignore).

No, I mean that: when I pass "ptr" to "clean" -in some particular way-
the compiler complains that data is intended to be deleted. Is there
such a way?

void clean( int* ptr )
{
delete ptr;
}
 
V

Victor Bazarov

D. Susman said:
No, I mean that: when I pass "ptr" to "clean" -in some particular way-
the compiler complains that data is intended to be deleted. Is there
such a way?

void clean( int* ptr )
{
delete ptr;
}

No. Don't pass a pointer. And don't hold onto a deleted pointer.

V
 
L

Lance Diduck

Thank you. Is there a way to make a pointer undeletable when passed to
a function?- Hide quoted text -
There are a number of ways. This is what I do:
void clean( int& ptr )
{
// delete ptr; //wont compile
ptr=0;
}


std::auto_ptr<int> ptr(new int(5));
clean(*ptr);
cout<<*ptr<<endl;

prints '0' always

Lance
 
J

James Kanze

There are a number of ways. This is what I do:
void clean( int& ptr )
{
// delete ptr; //wont compile

But "delete &ptr" will.
std::auto_ptr<int> ptr(new int(5));
clean(*ptr);
cout<<*ptr<<endl;
prints '0' always

In the end, a function must fulfill a defined contract. If you
don't know what the contract is, you don't use the function. If
the function's contract says it deletes the memory, you only
pass it pointers to stuff you want deleted. And if the
function's contract doesn't say that it deletes the memory, then
it had better not delete it, since you're quite likely to pass
it the address of a local variable, or whatever.

There are only a very limited number of contractual constraints
that can be expressed in the language. As a programmer, you
need to respect all of the contractual constraints, not just
those expressed in the language.
 
L

Lance Diduck

In the end, a function must fulfill a defined contract. If you
don't know what the contract is, you don't use the function. If
the function's contract says it deletes the memory, you only
pass it pointers to stuff you want deleted.
[snip]
There are only a very limited number of contractual constraints
that can be expressed in the language.
I couldn't agree more. I have seen many people confuse themselves over
the relation between syntax and correctness, esp over '=' and '=='. It
seems that because the algebra textbook and C++ syntax uses these same
glyphs, then they MUST both represent some Platonic notion of
"sameness." Of course there is a correlation, mostly because the early
programming languages were just trying to solve equations, and the
syntax carried on to this day. But that is where the similarity ends.
That said, syntax combined with simple rules goes a long way. A good
rule is to never call delete in the first place. This simple rule
squashes quite a few bugs. Couple that with "don't ever take the
address of an object" and you are well on your way to never having to
worry about memory errors.
But you are right, that there is far more to program correctness that
can be expressed in the language itself.
Lance
 
J

James Kanze

On Mar 11, 6:01 am, James Kanze <[email protected]> wrote:

[...]
That said, syntax combined with simple rules goes a long way.
A good rule is to never call delete in the first place. This
simple rule squashes quite a few bugs. Couple that with "don't
ever take the address of an object" and you are well on your
way to never having to worry about memory errors.

That's an interesting idea. It probably goes a bit further that
even I'd like, but...

-- use a garbage collector,
-- forbid dynamic allocation of objects with non-trivial
destructors, and
-- forbid taking the address of an existing object (so that all
pointers are to dynamically allocated objects).

The resulting program achieves true type safety. (I'm not sure
how you'd handle entity objects in it, though. And maybe I'm
just used to it, but I can't quite imagine never taking the
address of a local object, either, though one has to admit that
it is fraught with danger.)
 
A

Andre Kostur

No, I mean that: when I pass "ptr" to "clean" -in some particular way-
the compiler complains that data is intended to be deleted. Is there
such a way?

void clean( int* ptr )
{
delete ptr;
}

(I assume you meant "... data is intended to not be deleted...")

Pass the object by reference. If the callee then attempts to delete the
object by taking its address, the callee deserves what they get.

Pass the pointer through some wrapper class (std::auto_ptr perhaps)? Same
as above.. if the callee still extracts the pointer and tries to delete it,
the callee deserves what they get.

If you meant "intended to be deleted", then declare the parameter as an
auto_ptr by value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top