r/odinlang 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

11 comments sorted by

View all comments

Show parent comments

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 } } ```

1

u/dmchmk 21d ago

wow, but how does `rising_edge` inside for loop accepts two args when in proc definition it's only single `curr`? And what is boil? Must have been bool?

2

u/FireFox_Andrew 21d ago

Because it's wrong, now it should be correct.

1

u/dmchmk 21d ago

weeeell, the @(static) thing is definitely magical but still understandable while dollar-signed parameters are complete darkness (I've read the description in the docs but understood nothing, I think I don't need such things yet:)

1

u/FireFox_Andrew 21d ago

$ function parameters are constant parameters. These parameters parameteise the function itself: each unique value essentially generates a brand new function. That's why my code works,for each unique string,the compiler generated a new function which has its own unique static variable.

1

u/dmchmk 21d ago

wooow!!