Topic: Delete or free a pointer
Author: Mark Williams <markw65@my-deja.com>
Date: 30 Nov 00 01:23:44 GMT Raw View
In article <3A23C542.48D04A00@sensor.com>,
Ron Natalie <ron@sensor.com> wrote:
> "Sergey P. Derevyago" wrote:
> >
> > IMHO the assert statements may fail (in a some whimmy way
:), but no
> > program
> > crashes are allowed by the standard. That's the portable way to deal
with
>
> The standard does NOT define what happens when you store into one
union
> member and retrieve the value with another (with the exception of POD
> structs with common initial members). You can't make any assumptions
as
> to what is going to happen here.
But it *does* guarantee that you can read the bytes of any object
through a character pointer. The fact that there *happens* to be a
character array member in the union does not preclude that.
>
> The safe way is to NOT attempt to use the values of pointers that have
> been deleted/freed
Accessing the values of said pointers is undefined. Accessing the value
representation is not. I cant think of any good reason to access the
value rep of a freed pointer... but it is certainly legal.
-------------
Mark Williams
Sent via Deja.com http://www.deja.com/
Before you buy.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: 30 Nov 00 01:29:36 GMT Raw View
Ron Natalie wrote:
> "Sergey P. Derevyago" wrote:
> > IMHO the assert statements may fail (in a some whimmy way :), but no
> > program crashes are allowed by the standard. That's the portable way to
> > deal with
>
> The standard does NOT define what happens when you store into one union
> member and retrieve the value with another (with the exception of POD
> structs with common initial members). You can't make any assumptions as
> to what is going to happen here.
Could you quote the appropriate part of the standard? IMHO I'm allowed
to use the char[] part of my union without any restrictions.
union ptrRep {
void* ptr;
char rep[sizeof(void*)];
} un1, un2;
un1.ptr=un2.ptr=p;
free(un1.ptr);
assert(memcmp(un1.rep, un2.rep, sizeof(ptrRep))==0);
> The safe way is to NOT attempt to use the values of pointers that have
> been deleted/freed
I don't use any pointers (that have been deleted/freed), I use the
_characters_ in my assert statement. The plain characters living in the union.
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: 2000/11/30 Raw View
Mark Williams wrote:
> Ron Natalie <ron@sensor.com> wrote:
> > The safe way is to NOT attempt to use the values of pointers that have
> > been deleted/freed
>
> Accessing the values of said pointers is undefined. Accessing the value
> representation is not. I cant think of any good reason to access the
> value rep of a freed pointer... but it is certainly legal.
>
I can imagine at least one good reason. Suppose we want to have a special
(safe) pair of new/delete. And the delete function should be able to inform us
whether the pointer passed was not allocated with our new or we are going to
delete it twice. It seems like we have to have a has_map of freed pointers.
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: 2000/11/28 Raw View
Mark Williams wrote:
The snipped code:
5.
union ptrRep {
void* ptr;
char rep[sizeof(void*)];
} un1, un2;
un1.ptr=un2.ptr=p;
delete un1.ptr;
assert(memcmp(un1.rep, un2.rep, sizeof(ptrRep))==0);
6.
union ptrRep {
void* ptr;
char rep[sizeof(void*)];
} un1, un2;
un1.ptr=un2.ptr=p;
free(un1.ptr);
assert(memcmp(un1.rep, un2.rep, sizeof(ptrRep))==0);
> I see no reference to 4 chars anywhere. Otherwise, yes, that is exactly
> what he is doing. And, by the (apparently irrelevant) argument above,
> un1.rep == (char*)&un1.ptr. Hence the behavior is defined by the
> standard (the memcmp compares the value represantations of the two
> void*'s). This is *not* undefined behavior, as you claimed, though it
> *is* implementation defined - on implementations with more than one
> value representation for a pointer, the memcmp could return false.
IMHO the assert statements may fail (in a some whimmy way :), but no
program
crashes are allowed by the standard. That's the portable way to deal with
freed
pointers.
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/28 Raw View
"Sergey P. Derevyago" wrote:
>
> IMHO the assert statements may fail (in a some whimmy way :), but no
> program
> crashes are allowed by the standard. That's the portable way to deal with
The standard does NOT define what happens when you store into one union
member and retrieve the value with another (with the exception of POD
structs with common initial members). You can't make any assumptions as
to what is going to happen here.
The safe way is to NOT attempt to use the values of pointers that have
been deleted/freed
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]