digitalmars.D.learn - Allocate a string via the GC
- JG (7/7) May 23 2022 Hi,
- Adam D Ruppe (6/13) May 23 2022 Why do you want that?
- bauss (3/18) May 23 2022 My guess is @nogc in which case your solution doesn't work.
- bauss (4/24) May 23 2022 Oops wait, I just saw that it says "via the GC" in the title. I
- JG (6/21) May 23 2022 I am writing an interpreter and I needed access to a string via
- bauss (5/28) May 23 2022 You can take the address of the string. .ptr should do it, BUT I
- Adam D Ruppe (5/9) May 23 2022 OK yeah, that's the main use case I'd think of, and that's also
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (24/31) May 23 2022 Pointers are not used for strings in d. string is an alias for
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
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
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:My guess is nogc in which case your solution doesn't work. Same with Ferhat's examples.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
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: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.On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote:My guess is nogc in which case your solution doesn't work. Same with Ferhat's examples.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
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: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.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
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: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?On Monday, 23 May 2022 at 09:38:07 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. Thanks.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
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
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