www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Allocate a string via the GC

reply JG <someone somewhere.com> writes:
Hi,

Is there any more standard way to achieve something to the effect 
of:

```d
   import std.experimental.allocator;
   string* name = theAllocator.make!string;
  ```
May 23 2022
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
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;
  ```
Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know.... but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
May 23 2022
next sibling parent reply bauss <jj_1337 live.dk> writes:
On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote:
 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;
  ```
Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know.... but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
My guess is nogc in which case your solution doesn't work. Same with Ferhat's examples.
May 23 2022
parent bauss <jj_1337 live.dk> writes:
On Monday, 23 May 2022 at 12:17:56 UTC, bauss wrote:
 On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote:
 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;
  ```
Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know.... but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
My guess is nogc in which case your solution doesn't work. Same with Ferhat's examples.
Oops wait, I just saw that it says "via the GC" in the title. I completely missed that until I had of course sent my other message.
May 23 2022
prev sibling parent reply JG <someone somewhere.com> writes:
On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote:
 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;
  ```
Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know.... but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
I am writing an interpreter and I needed access to a string via a pointer of type void* I ended up wrapping it in a struct since I needed another value anyway. Seems odd that one can't do it in a less unusual way. Thanks.
May 23 2022
next sibling parent bauss <jj_1337 live.dk> writes:
On Monday, 23 May 2022 at 12:20:11 UTC, JG wrote:
 On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote:
 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;
  ```
Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know.... but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
I am writing an interpreter and I needed access to a string via a pointer of type void* I ended up wrapping it in a struct since I needed another value anyway. Seems odd that one can't do it in a less unusual way. Thanks.
You can take the address of the string. .ptr should do it, BUT I think it might not always work, someone can correct me on this, but I believe it depends on where the memory for the string lives whether it works or not?
May 23 2022
prev sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 23 May 2022 at 12:20:11 UTC, JG wrote:
 I am writing an interpreter and I needed access to a string via
 a pointer of type void*

 I ended up wrapping it in a struct since I needed another value
 anyway. Seems odd that one can't do it in a less unusual way.
OK yeah, that's the main use case I'd think of, and that's also exactly why I think doing the struct is the best thing anyway since you can bundle whatever you need in there instead of just a single value.
May 23 2022
prev sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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; ```
May 23 2022