Array slice length confusion

BCS none at anon.com
Thu Jul 9 07:34:47 PDT 2009


Hello Tim,

> BCS wrote:
> 
>> Hello Tim,
>> 
>>> Anyway I like how you can get a slice of array that is just a view
>>> into the original. If I modify the slice, it modifies the full
>>> original array which can be very useful. If however I modify the
>>> length of the slice which I was doing in an attempt to extend the
>>> view, it becomes it's own array.
>>> 
>> [...]
>> 
>>> Was this a design choice, bug, undefined behavior, etc...?
>>> 
>> you can grab a larger chunk like this
>> 
>> char[] buff1 = something;
>> char[] buff2 = buff1[10..20];
>> buff2 = buff2.ptr[0..20];
>> 
>> note this is unsafe and will give you no warnings if you extend past
>> the end of buff1;
>> 
> I know that but I when I said I was trying to extend the view I really
> mean that I was just trying to extend the view.

If that doesn't "extend the view", then I'm not understanding what you are 
asking for.

the above should compile to the sudocode:

t = &(buff.ptr[0]);
L = 20-0;
buff2.{ptr, length} = {t, L};

and should optimize to just an assignment to length.

> Also depending on what
> news reader you use you should see that it branched off in a
> particular direction to a solution.
> 

If you are referring the your response to Steven Schveighoffer, that amounts 
to the same thing I proposed but is even less safe (I don't think the layout 
of an array reference is speced) and harder to understand.




More information about the Digitalmars-d-learn mailing list