Questions about initialization of array of objects

bearophile bearophileHUGS at lycos.com
Sat Jan 12 12:18:58 PST 2013


I don't know the answers to those difficult questions. So I offer 
only partial tentative comments.

Maxim Fomin answering Andrey:

>> ClassPoint[8] arr; //static array of null references;

I prefer to call them fixed sized arrays, because the "static" 
word is too much overloaded in D.


>> StructPoint[8] arr2;
>>
>> arr[0].x=1 //error, there is no object
>> arr2[0].x=1 //OK.
>>
>> Here I have two questions:
>>
>> 1) Can you have automatically constructed objects with default 
>> class constructor?
>
> No. It is possible to create a wrapper struct type with "alias 
> this" to ClassPoint[8], which defaults to preallocated objects, 
> but as a drawback each instance of such type would default to 
> same class references and this makes the hack mostly unusable.

If ClassPoint has just a default constructor and the original 
poster wants to allocate those class instances in place, then 
maybe it's possible to invent something like this, that creates 
initialized class instances, but I think it's not very useful:

Emplace!(ClassPoint)[8] arr;


>> 2) Can you setup memory attributes for static array using 
>> core.memory functions, say, to force GC not to scan this 
>> portion of memory during collection?
>
> I do not understand what you mean here.

I think currently you can't tell the GC to ignore part of the 
stack. Fixed sized arrays are sometimes on the stack. So maybe 
it's not possible.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list