From -9146027423652055940
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,17893fccd2b995cb,start
X-Google-Attributes: gidf78e5,public
From: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Subject: Re: Memory layout (Was: Re: Pointer details)
Date: 1999/03/15
Message-ID: <7ci361$dg$1@engnews1.eng.sun.com>#1/1
X-Deja-AN: 455258315
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <36e6ddf0@news.uni-rostock.de>  <7c9kse$cqi$1@engnews1.eng.sun.com> <36EC0717.DF2@wanadoo.fr>
X-Original-Date: 15 Mar 1999 04:43:45 GMT
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 921513967 5478 128.250.29.16 (15 Mar 1999 16:06:06 GMT)
Organization: Sun Microsystems Inc., Mountain View, CA
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANu0v1+EDnX0m9pzZAQGb3QF/cj40gnIAGoaprf0W9ghBjGfnRGJpcbEO FtXb61DRuEVseEqZFtmgkv2OUhpHVFoO =OkRT
NNTP-Posting-Date: 15 Mar 1999 16:06:06 GMT
Newsgroups: comp.std.c++

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              ]



