RFC, ensureHeaped

bearophile bearophileHUGS at lycos.com
Fri Nov 12 09:58:27 PST 2010


Steven Schveighoffer:

> int[] globalargs;
> 
> void foo(int[] args...)
> {
>     globalargs = args;
> }
> 
> void bar()
> {
>     foo(1,2,3); // passes stack data to foo.
> }
> ...
> Then you just wasted time duping that argument.  Instead of a defensive  
> dup, what if we had a function ensureHeaped (better name suggestions?)  
> that ensured the data was on the heap?

Another possible solution, this turns some cases of stack assignment into a syntax error (wrong memory zone assignment error), and turns the undeterminable ones in a runtime test + optional allocation:

@onheap int[] globalargs;

Another possible solution is to modify the semantics of this kind of arguments pass, so the code inside the function foo always see an args array allocated on the heap:

void foo(int[] args...) { // code

You may then add "scope" to restore the original lighter semantics:
void foo(scope int[] args...) { // code

This is safer than the current semantics because the safe design is the built-in one and the faster is on request.

Bye,
bearophile


More information about the Digitalmars-d mailing list