More on C++ stack arrays

Tove tove at fransson.se
Mon Oct 21 15:00:38 PDT 2013


On Monday, 21 October 2013 at 01:48:56 UTC, Walter Bright wrote:
> On 10/20/2013 5:59 PM, Jonathan M Davis wrote:
>> If that paradigm is frequent enough, it might be worth 
>> wrapping it in a
>> struct. Then, you'd probably get something like
>>
>> StaticArray!(int, 10) tmp(n);
>> int[] a = tmp[];
>>
>> which used T[10] if n was 10 or less and allocated T[] 
>> otherwise. The
>> destructor could then deal with freeing the memory.
>
> Sounds like a good idea - and it should fit in with Andrei's 
> nascent allocator design.

Hmmm, it gave me a weird idea...

void smalloc(T)(ushort n, void function(T[]) statement)
{
   if(n <= 256)
   {
     if(n <= 16)
     {
       T[16] buf = void;
       statement(buf[0..n]);
     }
     else
     {
       T[256] buf = void;
       statement(buf[0..n]);
     }
   }
   else
   {
     if(n <= 4096)
     {
       T[4096] buf = void;
       statement(buf[0..n]);
     }
     else
     {
       T[65536] buf = void;
       statement(buf[0..n]);
     }
   }
}

smalloc(256, (int[] buf)
{
});


More information about the Digitalmars-d mailing list