Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 24, 2006 Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
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 Re: Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme | "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 Re: Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme | 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 Re: Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom S | 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 Re: Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: Dup'ing an array onto the stack | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom S | "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. |
Copyright © 1999-2021 by the D Language Foundation