Topic: Memory layout (Was: Pointer details)
Author: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/03/15 Raw View
Valentin Bonnard <Bonnard.V@wanadoo.fr> writes:
>Steve Clamage wrote:
>> Note, however, that pointers to class objects have other concerns.
>> A pointer to a base-class subobject is not necessarily a pointer
>> to the entire (most-derived) object. In addition, a pointer to
>> the entire class object is not necessarily a pointer to the first
>> byte of the object. (For example, some implementations have put
>> the vtable pointer at at negative offset.)
>Hum... could you please elaborate ?
>Do you mean that the following isn't garantied to pass:
>void* pv = operator new (sizeof (T)); // #1
>T* pt = new (pv) T; // #2
>assert (pt == pv);
If an implementation chooses to put the vpointer at a negative
offset, it has to go to some trouble to make everything work
right. In particular, a static_cast to or from void* must
adjust the pointer value so that you can allocate and
deallocate space with operator new and operator delete. Casting
to void* must yield the address of the start of storage, and
casting from void* must yield what is considered to be the
object's address.
In #1 above, the operator new returns a void*, assigned to a pv.
That is the address of the beginning of a storage area.
In #2, the new-expression returns the address of the T object.
The address of T will be offset from the beginning of the
storage area.
The comparison compares a T* to a void*. Since the types are
different, the T* must be converted (via an implicit static_cast)
to a void*, adjusting the pointer value back to point to the
beginning of storage. The pointers will then compare equal,
just as you would expect.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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 ]