C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Taking the address of a deleted pointer
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Andrei Alexandrescu (See
Guest





PostPosted: Mon Dec 13, 2004 10:35 am    Post subject: Taking the address of a deleted pointer Reply with quote



With the voice of a thousand thunders, 3.7.3. para 4 says:

----------------------
If the argument given to a deallocation function in the standard
library is a pointer that is not the null pointer value (4.10), the
deallocation function shall deallocate the storage referenced by the
pointer, rendering invalid all pointers referring to any part of the
deallocated storage. The effect of using an invalid pointer value
(including passing it to a deallocation function) is undefined.
(footnote 33)
----------------------

The associated footnote says:

----------------------
33) On some implementations, it causes a systemgenerated
runtime fault.
----------------------

It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.


Thanks,

Andrei

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Peter Koch Larsen
Guest





PostPosted: Tue Dec 14, 2004 3:33 am    Post subject: Re: Taking the address of a deleted pointer Reply with quote



Hi Andrei

"Andrei Alexandrescu (See Website for Email)"
<seewebsiteforemail (AT) moderncppdesign (DOT) com> skrev i en meddelelse
news:41BCF794.8080507 (AT) moderncppdesign (DOT) com...
Quote:
With the voice of a thousand thunders, 3.7.3. para 4 says:

----------------------
If the argument given to a deallocation function in the standard
library is a pointer that is not the null pointer value (4.10), the
deallocation function shall deallocate the storage referenced by the
pointer, rendering invalid all pointers referring to any part of the
deallocated storage. The effect of using an invalid pointer value
(including passing it to a deallocation function) is undefined.
(footnote 33)
----------------------

The associated footnote says:

----------------------
33) On some implementations, it causes a systemgenerated
runtime fault.
----------------------

It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

If you look at the computer, you're sitting at, there is a large probability
that it has a processor with the aforementioned mechanism. The '386-family
will cause a segment-fault if one of its segments is loaded with an invalid
value. A compiler is allowed to pass variables such as pointers in
registers, and if it decides to pass a pointer e.g. in FS:EBX (FS and EBX
are register names), the trap could occur.
While this is not a likely situation with the memory-model used by most
Windows-type operating system, it could occur if you run under some version
for a '286-type processor or (more likely) if you took advantage of using
what is known as AWE (an extension that allows programs to access more than
4GB of memory on some systems).
Quote:

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.
While I do not have access to the official standard I have a draft and I

have not seen anywhere where the draft disallows taking the adress of a
pointer. Also, I would be most surprised if this should be disallowed in the
"proper" standard. There are numerous places where taking the adress of an
uninitialised pointer gives meaning - all cases where you might want to
initialise it.
Quote:


Thanks,

Andrei
/Peter



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Tue Dec 14, 2004 3:35 am    Post subject: Re: Taking the address of a deleted pointer Reply with quote



Andrei Alexandrescu (See Website for Email) wrote:
Quote:
It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

All Intel processors beginning with the 286. This is less apparent with
the 32-bit processors, because most OS's never change the values in the
segment registers, which makes it look like they're working with a flat
address space. The full address is 48 bits, though: 16 bits for the
segment selector and 32 bits for the offset. If you're using 48-bit
addresses, an invalid selector value causes a trap; with a valid
selector an invalid offset also causes a trap. (If you're running on a
386 you've got a 16-bit selector and a 16-bit offset, and this is a much
more significant issue).

Quote:

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken?

Yes. It's just a data object.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Dec 14, 2004 7:48 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

Andrei Alexandrescu (See Website for Email) wrote:
Quote:
With the voice of a thousand thunders, 3.7.3. para 4 says:

----------------------
If the argument given to a deallocation function in the standard
library is a pointer that is not the null pointer value (4.10), the
deallocation function shall deallocate the storage referenced by the
pointer, rendering invalid all pointers referring to any part of the
deallocated storage. The effect of using an invalid pointer value
(including passing it to a deallocation function) is undefined.
(footnote 33)
----------------------

The associated footnote says:

----------------------
33) On some implementations, it causes a systemgenerated
runtime fault.
----------------------

It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();

int *p1 = new int(), *p2 = new int();

Quote:
if (p1 != p2) { ... } // fine
foo(p1); // fine

Depends on what 'foo' is, really. :-)

Quote:
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

Of course. Nothing happened to the storage for 'p1' itself. 'p1' is
an lvalue, and the address of it can be taken at any point after its
definition.

Quote:
I am looking for the appropriate standard text that allows or
disallows that.

Uh... Any relevant section on 'lvalue' and 'operator &', maybe?...

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Krügler (ne Spange
Guest





PostPosted: Tue Dec 14, 2004 7:52 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

Hello Andrei Alexandrescu,

Andrei Alexandrescu (See Website for Email) schrieb:
[snip]

Quote:
Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.

The last assignment seems reasonable. There was a similar discussion

"Using the pointer after delete"
in comp.std.c++ and the result was, that the last step should be at
least possible using the (unsigned) char*
"interface" of &p1.

Please note, that 5.3.5/4 effectively seems to make many possible usages
of that address
implementation-defined.

Quote from Andrew Koenig:

"5.3.5/4: The value of a pointer that refers to deallocated storage is
indeterminate.

That suggests to me that when you deallocate storage, the implementation is
permitted to scribble any pointer that formerly referred to that storage."

Greetings from Bremen,

Daniel Krügler




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrew Koenig
Guest





PostPosted: Tue Dec 14, 2004 7:56 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

"Andrei Alexandrescu (See Website for Email)"
<seewebsiteforemail (AT) moderncppdesign (DOT) com> wrote


Quote:
It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

The old Control Data computers used to have separate address and data
registers. Whenever you loaded an address into an address register it would
automagically fetch the contents of that address into the corresponding data
register. Those machines didn't have virtual memory, but it's not hard to
imagine a successor machine that would.

I believe that the Intel 286 worked similarly. Every address had two parts:
a segment and an offset. Loading a segment into a segment register would
attempt to access that segment; if it was not in the segment table, a fault
would result.

Now consider a C++ implementation that copies pointers by using the address
(on Control Data) or segment (on Intel) registers. Is such an
implementation valid? The point of the preceding paragraph is to say yes.

Quote:
Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.

I don't see any problem here -- it shouldn't be any different from taking
the address of an uninitialized local variable.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Tue Dec 14, 2004 8:00 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

Andrei Alexandrescu (See Website for Email) wrote:
Quote:
With the voice of a thousand thunders, 3.7.3. para 4 says:
...
It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.

Since p1 occupies storage, it should have a valid address, even if
the value stored at that address is invalid and subject to
super-duperisms. The worrisome expression would involve *pp1;
for example, if you add the line:

*pp1 = new int(); // fine??? *pp1 is not a valid value.

But even here, we are generating a valid reference (to the cell
occupied by p1's invalid value), and then writing through that
reference, without actually loading the dangerous value.

Life can get seriously uncomfortable if such actions are excluded
by the standard, or by super-duper registers.

And, if that were the case, this should work:

{ int *tmp = new int();
memset(pp1, sizeof(*pp1), &tmp);
}

--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tom Widmer
Guest





PostPosted: Tue Dec 14, 2004 8:05 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

On 13 Dec 2004 05:35:47 -0500, "Andrei Alexandrescu (See Website for
Email)" <seewebsiteforemail (AT) moderncppdesign (DOT) com> wrote:

Quote:
With the voice of a thousand thunders, 3.7.3. para 4 says:

----------------------
If the argument given to a deallocation function in the standard
library is a pointer that is not the null pointer value (4.10), the
deallocation function shall deallocate the storage referenced by the
pointer, rendering invalid all pointers referring to any part of the
deallocated storage. The effect of using an invalid pointer value
(including passing it to a deallocation function) is undefined.
(footnote 33)
----------------------

The associated footnote says:

----------------------
33) On some implementations, it causes a systemgenerated
runtime fault.
----------------------

It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper register
that is automatically checked at all times. As soon as an invalid
pointer is loaded into that super duper register, the super duper
hardware checker that comes bundled with the super duper register will
generate an access violation interrupt. (If anyone knows what
processors embed such a mechanism, I'd be indebted.)

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.

Using p1 as an lvalue (as above) should be fine, since it doesn't
involve accessing the pointer "value" (which I think can be safely
interpreted as "rvalue"). The standard perhaps needs to changing to
"The effect of using an invalid pointer rvalue ..." This is also
relevent to uninitialized variables and singular iterators, and the
standard could certainly be tidied up with regard to these. The
"lvalue-to-rvalue" conversion is the operation that should trigger UB,
but I'm not sure whether the standard is explicit enough about which
operations trigger an lvalue-to-rvalue conversion (e.g. most
operators).

There's already a defect report on the issue:
http://www2.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#312

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Tue Dec 14, 2004 8:08 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

Not sure why you find this confusing.

The auto variable p1 is of type pointer to int. Like any other
variable, it has an address. That address does not change during the
lifetime of p1.

Sometimes p1 contains a valid pointer, and sometimes it contains an
invalid one. This has nothing to do with the address of p1 itself.
Yes, loading that address into pp1 should be fine.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gary Labowitz
Guest





PostPosted: Wed Dec 15, 2004 4:43 am    Post subject: Re: Taking the address of a deleted pointer Reply with quote

"Daniel Krügler (ne Spangenberg)" <dsp (AT) bdal (DOT) de> wrote

Quote:
Hello Andrei Alexandrescu,

Andrei Alexandrescu (See Website for Email) schrieb:
[snip]

Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

<

Quote:
"5.3.5/4: The value of a pointer that refers to deallocated storage is
indeterminate.

That suggests to me that when you deallocate storage, the implementation
is
permitted to scribble any pointer that formerly referred to that storage."

I will assume you mean the implementation is allowed to modify the contents
of the pointer through which the delete was done.
If there were several pointers set to the same object, I doubt that the
implementation could keep track of them and "negate" them. I suppose it
might be possible if each time a pointer was assigned the source of the
assignment was traced back to the object and a list were kept. It just
doesn't seem worth doing.
--
Gary



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
msalters
Guest





PostPosted: Wed Dec 15, 2004 1:08 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote


Gary Labowitz wrote:

Quote:
I will assume you mean the implementation is allowed to modify the
contents
of the pointer through which the delete was done.
If there were several pointers set to the same object, I doubt that
the
implementation could keep track of them and "negate" them. I suppose
it
might be possible if each time a pointer was assigned the source of
the
assignment was traced back to the object and a list were kept. It
just
doesn't seem worth doing.

Worse, not only do you have to deal with the delete'd pointer value,
but also to all other objects that can be used to recalculate it.
In particular:

int* p = new int;
int* afterp = p + sizeof(int); //legal
delete p;
p = afterp - sizeof(int); // restore

Also, void*->int->void* conversions may exist.

Regards,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Dec 15, 2004 4:49 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

Andrew Koenig wrote:
Quote:
"Andrei Alexandrescu (See Website for Email)"
[email]seewebsiteforemail (AT) moderncppdesign (DOT) com[/email]> wrote in message
news:41BCF794.8080507 (AT) moderncppdesign (DOT) com...

It has been discussed here in the past how simply loading a pointer
(without actually dereferencing it) involves some super duper
register that is automatically checked at all times. As soon as an
invalid pointer is loaded into that super duper register, the super
duper hardware checker that comes bundled with the super duper
register will generate an access violation interrupt. (If anyone
knows what processors embed such a mechanism, I'd be indebted.)

The old Control Data computers used to have separate address and data
registers. Whenever you loaded an address into an address register
it
would automagically fetch the contents of that address into the
corresponding data register. Those machines didn't have virtual
memory, but it's not hard to imagine a successor machine that would.

I believe that the Intel 286 worked similarly. Every address had two
parts: a segment and an offset. Loading a segment into a segment
register would attempt to access that segment; if it was not in the
segment table, a fault would result.

It did. As does all of its successors. Most OS's ignore the segment
(4
Gbytes is enough for anyone), but not all. I have used a real-time OS
on an 80386 which supported 48 bit addresses, allocated each memory
block in a separate segment, and invalidated the segment when the
memory
was deallocated. And with the Intel compiler we were using (C, at the
time), any access to a pointer (as an rvalue) loaded the segment part
of
the address into a segment register. And I have really seen segment
faults due to accessing a freed pointer.

Quote:
Now consider a C++ implementation that copies pointers by using the
address (on Control Data) or segment (on Intel) registers. Is such
an
implementation valid? The point of the preceding paragraph is to say
yes.

I believe that the paragraph in question was added to the C standard at
the insistence of Intel, because their compiler/real-time OS did trap
if
you even looked at a freed pointer. The two most wide spread OS's for
the Intel architecture today (Windows and Linux) impose a linear
addressing, with the segments fixed once and for all, but I would be
very surprised if this is true for all compiler/OS pairs -- part of the
original intent of the segmented architecture was the added protection
(don't forget that at the time, Intel attempted to design a processor
--
the 432, whose machine language was Ada), and this is still relevant
for
certain types of applications.

The down side, of course, was that loading a valid pointer, even if
just
to compare it to null, loaded a segment register, and loading a segment
register loaded the segment descriptors, three or four memory accesses.
I think later compilers replaced the LES/LFS/LGS instructions with two
MOV instructions if no access was intented, to avoid this extra cost.
This, of course, would mean that you could read a pointer without
trapping.

Quote:
Now, one thing that I can't figure out from the text above is, what
about the address of a deleted pointer? Can it still be taken? For
example:

int* p1 = new int(), p2 = new int();
if (p1 != p2) { ... } // fine
foo(p1); // fine
int** pp1 = &p1; // fine
delete p1; // p1 is write-only
if (p1 != p2) { ... } // no!
foo(p1); // no!
pp1 = &p1; // still fine?

I am looking for the appropriate standard text that allows or
disallows that.

I don't see any problem here -- it shouldn't be any different from
taking the address of an uninitialized local variable.

I believe that the standard talks about the pointer having an invalid
value. My interpretation is that accessing the value implies an lvalue
to rvalue conversion, and vice versa, so that as long as no lvalue to
rvalue conversion is involved, you're safe.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Wed Dec 15, 2004 4:49 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

msalters wrote:

Quote:
int* p = new int;
int* afterp = p + sizeof(int); //legal

OK.

Quote:
delete p;

OK, now p and afterp are invalid.

Quote:
p = afterp - sizeof(int); // restore

Bang! Rest In Pieces.

Above, afterp is an iterator pointing into the array (it is past-the-end
iterator) that is invalidated by deleting the array itself.
See 3.7.3.2/4:

"the deallocation function shall deallocate the storage referenced by
the pointer, rendering invalid all pointers referring to ANY PART of the
deallocated storage."

I think that past-the-end pointer belongs to the "any part of the
storage" category.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Wed Dec 15, 2004 4:49 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote


"msalters" <Michiel.Salters (AT) logicacmg (DOT) com> schrieb im Newsbeitrag
news:1103103472.236419.149970 (AT) z14g2000cwz (DOT) googlegroups.com...
Quote:

Gary Labowitz wrote:

I will assume you mean the implementation is allowed to modify the
contents
of the pointer through which the delete was done.
If there were several pointers set to the same object, I doubt that
the
implementation could keep track of them and "negate" them. I suppose
it
might be possible if each time a pointer was assigned the source of
the
assignment was traced back to the object and a list were kept. It
just
doesn't seem worth doing.

Worse, not only do you have to deal with the delete'd pointer value,
but also to all other objects that can be used to recalculate it.
In particular:

int* p = new int;
int* afterp = p + sizeof(int); //legal


Sure about that for all (including some very popular) machines?


Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Dec 15, 2004 4:51 pm    Post subject: Re: Taking the address of a deleted pointer Reply with quote

msalters wrote:
Quote:
Gary Labowitz wrote:

I will assume you mean the implementation is allowed to modify the
contents of the pointer through which the delete was done. If
there
were several pointers set to the same object, I doubt that the
implementation could keep track of them and "negate" them. I
suppose
it might be possible if each time a pointer was assigned the source
of the assignment was traced back to the object and a list were
kept. It just doesn't seem worth doing.

Worse, not only do you have to deal with the delete'd pointer value,
but also to all other objects that can be used to recalculate it. In
particular:

I think I agree with what I think you are trying to say, but...

Quote:
int* p = new int;
int* afterp = p + sizeof(int); //legal

Undefined behavior. p+1 is legal, though.

Quote:
delete p;
p = afterp - sizeof(int); // restore

Undefined behavior. After the delete, you can not access afterp
either.

But I think your point was that it would be difficult for an
implementation to ensure that this really failed.

Quote:
Also, void*->int->void* conversions may exist.

Supposing the implementation supports them. (Mine doesn't, for the
simple reason that pointers are 64 bits, but ints are only 32.) But in
another thread, there was discussion of copying a pointer into a char[]
buffer with memcpy, then reading it back in the same manner. Perhaps
having encrypted and decrypted the buffer in between.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.