Wrapping a C-style Array (Pointer + Length) in a Range Interface

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 7 05:36:09 PDT 2015


On Tuesday, 7 July 2015 at 12:33:23 UTC, Per Nordlöw wrote:
> On Tuesday, 7 July 2015 at 12:29:04 UTC, Rikki Cattermole wrote:
>> size_t count;
>> AVStream* thePtr;
>> AVStream[] array = thePtr[0 .. count];
>>
>> That should work.
>
> Thanks.
>
> Will that reuse the existing allocate memory at `thePtr` for 
> internal storage of the D array? If so how is the GC aware of 
> this memory?
>
> Is there any tutorials, reference documentation, etc on these 
> matters?

To my knowledge it should reuse the memory and not inform the GC 
about it. It basically makes D treat that pointer like a slice.

If you need to confirm that it is doing that, just @nogc it. If 
it allocates it'll fail.

I'm not aware of much docs regarding these sort of tricks. But I 
believe it was Adam who originally discovered this little trick. 
Atleast I'm pretty sure he is how I learnt about it. On D.learn 
as well.


More information about the Digitalmars-d-learn mailing list