Convert C array pointer to a D slice without data copy

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 02:23:25 PDT 2015


On Monday, 18 May 2015 at 09:18:33 UTC, ParticlePeter wrote:
> I get the point to an array from a c function, the data size 
> from another function. The data should be only readable at the 
> D side, but I would like to use it as a D slice without copying 
> the data. Is this possible ?

char* dataPtr;
size_t dataLen;

auto data = dataPtr[0 .. dataLen];

This doesn't do any copying. BUT I am not sure what GC would be 
doing about it. After you use it, you might want to set `data` to 
null in case of a problem.


More information about the Digitalmars-d-learn mailing list