Any way to set len and ptr in dyn arrays?
nobody
nobody at mailinator.com
Wed Aug 9 14:33:06 PDT 2006
BCS wrote:
> Frank Benoit wrote:
>>>> This builds a slice, which is like setting ptr/length of an array. This
>>>> is called slicing. Instead of Toms example, I think you can also write
>>>> this:
>>>> foo = buf[ 2 .. buf[0] * buf[1] + 2 ];
>>>>
>>>
>>> Thanks to you also for your response. I was trying to suggest before
>>> that I understand how slicing ends up -- ie with ptr and len as I
>>> desired. I am worried about what happens while the slicing happens. I am
>>> worried about any gotchas.
>>
>>
>> I my understanding this is exactly like setting the ptr and length.
>> One side effect could be, that the compiler perhaps inserts array
>> boundary check. But I don't know that.
>
> I don't think that any extra overhead is involved in slicing. D doesn't
> even check if a pointer is valid. (That might be a gotcha come to think
> of it)
>
> void main()
> {
> char[] foo = (cast(char*)null)[0..10];
> }
Great example! I am now convinced there is nothing to worry
about. Thanks everyone for the help!
More information about the Digitalmars-d-learn
mailing list