RFC, ensureHeaped

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Nov 12 09:25:56 PST 2010


On 11/12/10 5:10 AM, Steven Schveighoffer wrote:
> I just recently helped someone with an issue of saving an array to stack
> data beyond the existence of that stack frame. However, the error was
> one level deep, something like this:
>
> int[] globalargs;
>
> void foo(int[] args...)
> {
> globalargs = args;
> }
>
> void bar()
> {
> foo(1,2,3); // passes stack data to foo.
> }
>
> One thing I suggested is, you have to dup args. But what if you call it
> like this?
>
> void bar()
> {
> foo([1,2,3]);
> }
>
> 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? If it wasn't, it dups the
> original onto the heap. It would be less expensive than a dup when the
> data is already on the heap, but probably only slightly more expensive
> than a straight dup when the data isn't on the heap.
>
> Would such a function make sense or be useful?
>
> -Steve

Sounds good, but if we offer it we should also define the primitive 
isOnStack() or something.

Andrei


More information about the Digitalmars-d mailing list