resizeable arrays: T[new]
Sean Kelly
sean at f4.ca
Wed Jun 6 23:21:41 PDT 2007
Walter Bright wrote:
> Sean Kelly wrote:
>> Given the above, am I correct in assuming that it will only be illegal
>> to resize an array via .length if the underlying data is const? If
>> the data is not const then it seems completely legitimate to resize
>> the supplied array, even if doing so means an in-place expansion.
>
> Making the referred to data const does not inhibit resizing, because
> resizing does not change the referred to data.
>
> The key to understanding dynamic arrays is understanding their
> representation. Dynamic arrays of const data are like a C++ struct
> defined as:
>
> struct DynamicArray
> {
> size_t length;
> const char *ptr;
> }
>
> Here, I can change .length and .ptr EVEN THOUGH the data being pointed
> to is const.
Okay, that makes sense. So I'm not sure I entirely understand the
problem with resizing even mutable array in place. Could this just be
an instance where documentation is indeed sufficient? I can't think of
a situation where I would actually pass a slice of a buffer to a routine
that may grow that buffer when writing to it--it just doesn't make any
sense from a use perspective. Is this something that we really need to
add const features to check?
Sean
More information about the Digitalmars-d-announce
mailing list