Address of a class object
H. S. Teoh
hsteoh at qfbox.info
Wed Jan 4 18:48:19 UTC 2023
On Wed, Jan 04, 2023 at 09:51:05AM -0800, Ali Çehreli via Digitalmars-d-learn wrote:
> On 1/3/23 20:01, Paul wrote:
>
> > Size Alignment Type
> > =========================
> > 17 8 MyClass
> >
> > MyClassObj1 MyClassObj2
> > 27727202000 27727202020
> > ```
> > If my size is 17 bytes and my alignment is 8 bytes, shouldn't my
> > MyClassObj2 in this example be @ 277272020**18** ?
>
> Good question.
>
> I made some guesses about object layouts, GC allocation schemes, etc.
> but did not like any of them.
[...]
Allocations are not necessarily consecutive; the GC may have its own
strategy of allocation that doesn't follow a linear sequence.
Furthermore, GC-allocated blocks may be larger than the request size
because there may be some extra management information stored in the
block (but outside the pointer range returned).
Basically, addresses returned by an allocator (GC or otherwise) should
be treated as opaque handles, whose exact values are implementation-
dependent and not really relevant to application code other than
identifying the allocated object.
T
--
We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true. -- Robert Wilensk
More information about the Digitalmars-d-learn
mailing list