#Do I need to clone a string when using `StringArrayHashMap`?
1 messages · Page 1 of 1 (latest)
sure, but idk what more context do you need tho
var documents = std.StringArrayHashMap(TextDocument).init(allocator);
documents.put(somestring, value);
somestring gets defer freed later on
and documents outlive it
bruh, the string may be comptime known or stack allocated or heap allocated
choose one
the string is heap allocated
no you don't need to dupe it
but it gets freed tho? and outlives documents?
it needs to be freed as documents is freed
that's the problem, they don't have the same lifetimes
ok then dupe it
std.StringArrayHashMap doesn't alloc/free keys for you, it's just a wrapper over ArrayHashMap that gives a hash function for strings. When in doubt, check the source code:
https://github.com/ziglang/zig/blob/89942ebd03b2943cbbe84b575a024e156ca5bf52/lib/std/array_hash_map.zig#L27
If the strings live for more than the map already then you don't need to dupe
The hash map doesn't manage the lifetime of its keys, no.
You're meant to be able to decide the lifetimes of these things yourself, and thus, the key needs to remain valid while it's being used as a key in the map.
depends on if the map outlived the key or if the key outlives the map?
here OP says map outlives key
here OP says key outlives map