More on C++ stack arrays

dennis luehring dl.soluz at gmx.net
Wed Oct 23 07:35:13 PDT 2013


Am 23.10.2013 15:59, schrieb Namespace:
> On Sunday, 20 October 2013 at 16:33:35 UTC, Walter Bright wrote:
>> On 10/20/2013 7:25 AM, bearophile wrote:
>>> More discussions about variable-sized stack-allocated arrays
>>> in C++, it seems
>>> there is no yet a consensus:
>>>
>>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3810.pdf
>>>
>>> I'd like variable-sized stack-allocated arrays in D.
>>
>> They're far more trouble than they're worth.
>>
>> Just use:
>>
>>     auto a = new T[n];
>>
>> Stack allocated arrays are far more trouble than they're worth.
>> But what about efficiency? Here's what I often do something
>> along the lines of:
>>
>>     T[10] tmp;
>>     T[] a;
>>     if (n <= 10)
>> 	a = tmp[0..n];
>>     else
>> 	a = new T[n];
>>     scope (exit) if (a != tmp) delete a;
>>
>> The size of the static array is selected so the dynamic
>> allocation is almost never necessary.
>
> Another idea would be to use something like this:
> http://dpaste.dzfl.pl/8613c9be
> It has a syntax similar to T[n] and is likely more efficient
> because the memory is freed when it is no longer needed. :)
>

but it would be still nice to change the 4096 size by template parameter 
maybe defaulted to 4096 :)


More information about the Digitalmars-d mailing list