Allocating a wstring on the stack (no GC)?

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Wed May 7 12:19:56 PDT 2014


On Wednesday, 7 May 2014 at 18:41:17 UTC, Maxime 
Chevalier-Boisvert wrote:
> 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.

Looks like you actually don't need to allocate anything at all. 
Any slice is just a struct with 2 fields, it does not own data 
and can be used as a view to anything:

void* some_external_data;
size_t external_length;

void foo()
{
     auto str = 
cast(wstring[])(some_external_data[0..external_length]);
     // this will create a stack instance of slice struct with 
`.ptr` field pointing to same address as `some_external_data`
}


More information about the Digitalmars-d mailing list