Converting void* to D array

CraigDillabaugh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 15 08:02:21 PDT 2015


On Wednesday, 15 April 2015 at 14:02:38 UTC, Steven Schveighoffer 
wrote:
> 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
Nice.  Thanks.  I didn't realize you can slice a bare pointer 
like that.

Does druntime have any way of making sure that is safe, or are 
you on your own?


More information about the Digitalmars-d-learn mailing list