Heap allocation and internal pointers

Dmitry Olshansky dmitry.olsh at gmail.com
Mon Jan 20 08:35:10 PST 2014


19-Jan-2014 20:18, monarch_dodra пишет:
> What is the "stance" on objects that reside on the heap, and internal
> pointers?
>
> I understand that for stack objects, it leads to data corruption, due to
> data being bit-moved when passed around.
>
> But what about heap data? Currently, our GC doesn't move data around,
> but what if it did? Would internal pointers be a problem? How do java/C#
> handle such cases?


>
> My usecase is pretty trivial: A linked list. This is often implemented
> as a "single" sentinel that serves as both pre-head/post-tail. When the
> list is empty, the sentinel simply points to itself.
>

You could use internal pointers for this case as long as:
a) The struct will never get copied and/or moved. No pass by value, only 
pointers.
b) It's out of GC control and in particular GC-allocated built-in arrays.

This means you don't have that much of choice beyond things like:
MyObject* node = (MyObject*)malloc(MyObject.sizeof);

And only ever using pointers.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list