GC.sizeOf(array.ptr)

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 30 06:42:12 PDT 2014


There is one issue I have encountered during CDGC porting I may 
need advice with.
Consider this simple snippet:

```
import core.memory;

void main()
{
     ubyte[] result;
     result.length = 4096;
     assert(GC.sizeOf(result.ptr) > 0);
}
``

Assertion passes with D1/Tango runtime but fails with current D2 
runtime. This happens because `result.ptr` is not actually a 
pointer returned by gc_qalloc from array reallocation, but 
interior pointer 16 bytes from the start of that block. Druntime 
stores some metadata (length/capacity I presume) in the very 
beginning.

As a result GC.sizeOf(array.ptr) results in 0 (being an interior 
pointer).

Interesting side effect is that this code:

```
void main()
{
     ubyte[] result;
     result.length = 4096;
     GC.free(result.ptr);
}
```

..does not actually free anything for the very same reason 
(result.ptr is interior pointer), it just silently succeeds doing 
nothing.

Is such behaviour intended?


More information about the Digitalmars-d mailing list