T[new]

Walter Bright newshound1 at digitalmars.com
Sun Aug 9 15:07:48 PDT 2009


Stewart Gordon wrote:
> Walter Bright wrote:
> <snip>
>> Under the hood, a T[new] will be a single pointer to a library defined 
>> type. This library defined type will likely contain three properties:
>>
>>     size_t length;
>>     T* ptr;
>>     size_t capacity;
>>
>> The usual array operations will work on T[new] as well as T[].
> <snip>
> 
> Would new T[10] allocate this structure and the array data on a single 
> GC block, or on two separate blocks?

That's up to the implementation.

> And when the array is reallocated, 
> will the structure move with it?

No, that would defeat the whole purpose of making T[new] a reference 
type. With it being a reference type:

T[new] a = ...;
T[new] b = a;

a.length = ...
... b.length changes too ...


> I suppose it depends on whether you want T[new] to be
> (a) something whereby all references persist as the array is reallocated 
> (b) merely a reference to an allocated array as opposed to an array slice
> 
> If (a), this is currently achievable with a T[]*.
> 
> If (b), what might work well is a structure like
> 
>     size_t length;
>     size_t capacity;
>     T[capacity] data;
> 
> meaning still only one allocation and only one level of indirection when 
> one is used.  And the T[new] variable itself would simply hold &data[0].
> 
> Moreover, would whatever happens solve such const/invariant holes as bug 
> 2093?

I believe it does.

> 
> Stewart.



More information about the Digitalmars-d mailing list