Allocating a wstring on the stack (no GC)?
bearophile via Digitalmars-d
digitalmars-d at puremagic.com
Wed May 7 11:50:11 PDT 2014
Maxime Chevalier-Boisvert:
> I do need it to be dynamically sized.
But often you can determine statically a maximum length of the
string, so you can use a fixed size stack buffer and slice it
with a dynamic length. If this is not acceptable, then use alloca.
> Basically, I just want to create a wstring "view" on an
> existing "raw" buffer that exists in memory somewhere, based on
> a pointer to this buffer and its length.
This is named slicing. You can also slice a
global/static/__gshared buffer.
> Side note: wouldn't alloca just produce a wchar*, not an array
> (wouldn't have length information)?
alloca returns a void*, then you can cast it to the pointer type
you want, and then you slice the pointer:
auto ptr = cast(wchar*)alloca(wchar.sizeof * len);
if (ptr == null) throw new Error("...");
auto mySlice = ptr[0 .. len];
Bye,
bearophile
More information about the Digitalmars-d
mailing list