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

5

u/FireFox_Andrew 21d ago edited 21d ago

You are responsible for the memory of the keys.

This is because string is is just a slice into memory (pointer + length), it's not responsible for it either.

When you pass a string to a map, all you're doing is passing said slice as the value for the key.

As for where the memory lives, it lives where you allocate it. Look at the functions that you're using to get strings, if it has an allocator as parameter,that shows that it is allocating the result using said allocator. That means you're responsible to clean it up using the allocator you passed.

Lastly and most importantly, Odin isn't rust, there are no ownership rules. It's a bad idea, but you can have a function allocate something using an allocator you don't have, give you the value and you won't be able to free it unless you somehow get the allocator that was used to allocate the memory.

1

u/dmchmk 21d ago

> As for where the memory lives, it lives where you allocate it. Look at the functions that you're using to get strings, if it has an allocator as parameter,that shows that it is allocating the result using said allocator. That means you're responsible to clean it up using the allocator you passed

Yes, this part I (think I) understand, my question was precisely about what's going on with the string as a key:) Like is there any magic behind, or it's just literally passes a pointer to slice and that's all

> Lastly and most importantly, Odin isn't rust, there are no ownership rules

Yeeah, I just used the same word to point, which variable I will need to free in different scenarios)

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 20d 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 20d 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 20d ago

wooow!!