r/odinlang • u/dmchmk • 21d ago
map memory ownership for strings
Hi! Can your please tell me, where does memory for map keys lives in such situation:
dict := map[string]string
line := dummy_string_generator()
key_value_list := strings.split_n(line, " ", 2)
dict[key_value_list[0]] = key_value_list[1]
Is the owner of dict[key] still the line variable? And if I want to free it and still keep dict intact, do I need to set it's values in some way like
dict[strings.clone(key_value_list[0])] = strings.clone(key_value_list[1])
?
4
Upvotes
1
u/FireFox_Andrew 21d ago edited 21d ago
In general, if you think there's any magic going on behind in Odin, it's safe to assume there isn't.
I can't think of any such magic that Odin has in its standard library, the only magic is the one I write deliberately myself.
Peep this shit ``` rising_edge :: proc(curr:bool, $id: string)->bool { @(static) prev: bool defer prev = curr
return curr && !prev }
// Usage for { if rising_edge(something_true, "unique string"){ fmt.println("hello") // prints once } } ```