www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dup'ing an array onto the stack

reply John Demme <me teqdruid.com> writes:
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
Feb 24 2006
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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?
Feb 24 2006
parent John Demme <me teqdruid.com> writes:
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
Feb 24 2006
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
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 +/
Feb 24 2006
next sibling parent John Demme <me teqdruid.com> writes:
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[]; ?

Feb 24 2006
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Feb 25 2006