Allocate a string via the GC
Ferhat Kurtulmuş
aferust at gmail.com
Mon May 23 11:42:27 UTC 2022
On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote:
> Hi,
>
> Is there any more standard way to achieve something to the
> effect of:
>
> ```d
> import std.experimental.allocator;
> string* name = theAllocator.make!string;
> ```
Pointers are not used for strings in d. string is an alias for
immutable(char)[]. So, you can do:
```d
import std.exception : assumeUnique; // makes char[] immutable
string str = (new char[15]).assumeUnique;
str = "hmm. my string!";
```
a slice in d (e.g. char[]) is roughly considered as a struct with
a pointer and length. I also suspect that your question is not
that simple, maybe you really need a string pointer, and I
couldn't understand what you are actually asking. I am sorry if
those are not what you were looking for the answers to.
Here is how you GC allocate a pointer to a string.
```d
string* strptr = new string[1].ptr;
// Since slices are reference types just use this instead:
string[] strptr = new string[1];
```
Maybe you need a pointer to the first char of a string then:
```d
string str; // allocated somehow
immutable(char)* chr = str.ptr;
```
More information about the Digitalmars-d-learn
mailing list