November 13, 2008 Re: Stack-allocated arrays | ||||
|---|---|---|---|---|
| ||||
Andrei Alexandrescu Wrote:
> With the SuperStack in place, code could look like this:
>
> void foo(in size_t s)
> {
> auto a = SuperStack.array(int)(s, Uninitialized.yeahIKnow);
> scope(exit) SuperStack.free(s);
> ... play with a ...
> }
void foo(in size_t s)
{
StackFrame m; //struct, remember heap status in constructor
//restore in destructor
//bonus: can't alloc unless StackFrame is created
auto a = m.alloc(int[])(s, Uninitialized.yeahIKnow);
//scope(exit) SuperStack.free(s); you don't need free to free stack
... play with a ...
}
| ||||
November 13, 2008 Re: Stack-allocated arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | Kagamin wrote:
> Andrei Alexandrescu Wrote:
>
>> With the SuperStack in place, code could look like this:
>>
>> void foo(in size_t s)
>> {
>> auto a = SuperStack.array(int)(s, Uninitialized.yeahIKnow);
>> scope(exit) SuperStack.free(s);
>> ... play with a ...
>> }
>
> void foo(in size_t s)
> {
> StackFrame m; //struct, remember heap status in constructor
> //restore in destructor
> //bonus: can't alloc unless StackFrame is created
> auto a = m.alloc(int[])(s, Uninitialized.yeahIKnow);
> //scope(exit) SuperStack.free(s); you don't need free to free stack
> ... play with a ...
> }
Sweet!!
Andrei
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply