Top 5

Denis Koroskin 2korden at gmail.com
Fri Oct 10 10:47:30 PDT 2008


On Fri, 10 Oct 2008 21:30:23 +0400, Benji Smith <dlanguage at benjismith.net>  
wrote:

> Denis Koroskin wrote:
>> On Fri, 10 Oct 2008 19:54:33 +0400, Benji Smith  
>> <dlanguage at benjismith.net> wrote:
>>
>>> I think *all* arrays should be declared like this:
>>>
>>>     T[] array = new T[n];
>>>
>>  You often want to avoid heap allocation at all cost. It can't be done  
>> as you propose.
>
> For all other reference types, allocation on the stack is accomplished  
> with the "scope" keyword, without having a different type, or a  
> different constructor-call syntax. I think the same thing could apply to  
> arrays just as easily.
>
> --benji

No, you can't create scoped arrays this way. Assume you have:

scope T[] array = new T[n];
array ~= t; // calls realloc(array.ptr);

If ptr points to stack space then you get an access violation.



More information about the Digitalmars-d mailing list