Thread overview
Dup'ing an array onto the stack
Feb 24, 2006
John Demme
Feb 25, 2006
John Demme
Feb 25, 2006
Tom S
Feb 25, 2006
John Demme
February 24, 2006
I need to .dup a string on to the stack.  I current have:
char[] str = ...;
char[] stackStr = str.dup;

But it's a lie, since stackStr is stored on the heap.  How do I get it on the stack?  I can easily get the space on the stack for it using alloca(), but how do I tell the array to use a certain place in memory?

Thanks,
John Demme
February 25, 2006
"John Demme" <me@teqdruid.com> wrote in message news:dto0oq$tpg$1@digitaldaemon.com...
>I need to .dup a string on to the stack.  I current have:
> char[] str = ...;
> char[] stackStr = str.dup;
>
> But it's a lie, since stackStr is stored on the heap.  How do I get it on the stack?  I can easily get the space on the stack for it using alloca(), but how do I tell the array to use a certain place in memory?

I don't think you can have dynamic arrays point to the stack without some ugly hacks.  They're not really meant to point at the stack, as that wouldn't work with the dynamic sizing.  Only static arrays can be created on the stack.

Why do you need it on the stack?


February 25, 2006
John Demme wrote:
> But it's a lie, since stackStr is stored on the heap.  How do I get it on
> the stack?  I can easily get the space on the stack for it using alloca(),
> but how do I tell the array to use a certain place in memory?

How about:

char[] foo = (cast(char*)alloca(...))[0 .. bar.length];
foo[] = bar[];

?


-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y
------END GEEK CODE BLOCK------

Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
February 25, 2006
That's the code I was looking for.  Thanks.

~John Demme

Tom S wrote:

> John Demme wrote:
>> But it's a lie, since stackStr is stored on the heap.  How do I get it on the stack?  I can easily get the space on the stack for it using alloca(), but how do I tell the array to use a certain place in memory?
> 
> How about:
> 
> char[] foo = (cast(char*)alloca(...))[0 .. bar.length];
> foo[] = bar[];
> 
> ?
> 
> 

February 25, 2006
Jarrett Billingsley wrote:
> 
> Why do you need it on the stack?

It's part of an XML parser for Mango... One of my goals is no memory allocations after object construction, and I'm getting pretty close.

~John
February 25, 2006
"Tom S" <h3r3tic@remove.mat.uni.torun.pl> wrote in message news:dto984$1inu$1@digitaldaemon.com...
> How about:
>
> char[] foo = (cast(char*)alloca(...))[0 .. bar.length];
> foo[] = bar[];
>

Huh!  Would've thought that that'd've copied the data from the original alloca'd buffer, but now that I think about it, I guess it wouldn't.