Converting void* to D array

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 15 07:02:38 PDT 2015


On 4/15/15 8:35 AM, CraigDillabaugh wrote:
> On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote:

>> immutable blocksize = GByte.sizeof * x_block_size * y_block_size;
>> auto buffer = malloc(blocksize)[0..blocksize];
>>
>> Also, you don't need to cast pointers to void *. Should be able to do:
>>
>> GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr);
>>
>
> Thanks for the pointers (no pun intended!)
>
> Just out of curiosity, what is the type of 'buffer'?  'malloc' returns a
> void* but using the .ptr suggests 'buffer' is an array.  Is the return
> of malloc automatically converted to an array?

malloc returns void *, but I am applying the slice operator which turns 
a pointer into a slice. I'll split it up:

auto bufptr = malloc(blocksize);    // typeof(bufptr) = void *
auto buffer = bufptr[0..blocksize]; // typeof(buffer) = void[]

-Steve


More information about the Digitalmars-d-learn mailing list