Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 02, 2019 Cast ptr/len to D-style array | ||||
---|---|---|---|---|
| ||||
I work a lot with C functions, many of which yield pointer + length. Is there a way to cast this or materialize a D-style array backed by the already allocated data (with length) to avoid copies which slow things down? I recognize memory management is a complication. Typically, I would be responsible for free'ing the array later. Thanks in advance |
May 02, 2019 Re: Cast ptr/len to D-style array | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Blachly | On 02.05.19 22:03, James Blachly wrote:
> I work a lot with C functions, many of which yield pointer + length.
>
> Is there a way to cast this or materialize a D-style array backed by the already allocated data (with length) to avoid copies which slow things down?
Just slice the pointer with the length:
int* ptr;
size_t len;
int[] arr = ptr[0 .. len];
|
May 02, 2019 Re: Cast ptr/len to D-style array | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On 5/2/19 4:05 PM, ag0aep6g wrote:
> Just slice the pointer with the length:
>
> int* ptr;
> size_t len;
> int[] arr = ptr[0 .. len];
Perfect thanks. I searched but without using the magic word "slice" I couldn't find meaningful results.
Thanks again.
|
May 03, 2019 Re: Cast ptr/len to D-style array | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Blachly | On 05/02/2019 08:21 PM, James Blachly wrote: > On 5/2/19 4:05 PM, ag0aep6g wrote: >> Just slice the pointer with the length: >> >> int* ptr; >> size_t len; >> int[] arr = ptr[0 .. len]; > > Perfect thanks. I searched but without using the magic word "slice" I couldn't find meaningful results. > > Thanks again. Looks like I indexed it under "slice from pointer": http://ddili.org/ders/d.en/pointers.html#ix_pointers.slice%20from%20pointer Ali |
May 04, 2019 Re: Cast ptr/len to D-style array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 5/3/19 5:42 AM, Ali Çehreli wrote:
> On 05/02/2019 08:21 PM, James Blachly wrote:
>> On 5/2/19 4:05 PM, ag0aep6g wrote:
>>> Just slice the pointer with the length:
>>>
>>> int* ptr;
>>> size_t len;
>>> int[] arr = ptr[0 .. len];
>>
>> Perfect thanks. I searched but without using the magic word "slice" I couldn't find meaningful results.
>>
>> Thanks again.
>
> Looks like I indexed it under "slice from pointer":
>
>
> http://ddili.org/ders/d.en/pointers.html#ix_pointers.slice%20from%20pointer
>
> Ali
>
Thanks Ali, also helpful examples!
|
Copyright © 1999-2021 by the D Language Foundation