memory/array question

Eric via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 31 13:07:00 PDT 2014


On Thursday, 31 July 2014 at 19:43:00 UTC, bearophile wrote:
> Eric:
>
>> Suppose I have some memory allocated on the heap, and I have
>> two pointers pointing to the beginning and end of a contiguous 
>> segment
>> of that memory.  Is there a way I can convert those two 
>> pointers
>> to an array slice without actually copying anything within the 
>> segment?
>
> Use something like this (but make sure the length is correct):
>
> auto mySlice = ptr1[0 .. ptr2 - ptr1];
>
> Bye,
> bearophile

Thanks. That really works.  I timed doing

auto mySlice = ptr1[0 .. ptr2 - ptr1]; 1,000,000 times

versus

auto mySlice = ptr1[0 .. ptr2 - ptr1].dup; 1,000,000 times

and I am quite convinced the data is not being copied.



More information about the Digitalmars-d-learn mailing list