I am new to odin and I am making a union of different types where all have have the same base struct (with “using”) and some have an extra stuff like a string or a dynamic array.
how should I go about setting up the deallocation function?
should I call it “free” or “delete”?
is it a good idea to overload “delete”?
is there a way to make it so that if the union is put inside of a dynamic array, freeing the dynamic array will free all of the unions? (is that even a good thing to do?)
A bit confused on what you are asking. Everything you are currently doing is on the stack and therefore doesn’t need to be deallocated.
There is no way to overload delete or free.
‘Free’ is used with things that you have allocated using ‘new’
‘Delete’ is used with things that you have allocated using ‘make’.
okay my question is a bit broken, I am allocating the strings to the heap because a procedure is returning these structs as a heap allocated dynamic array where their strings are also heap allocated.
I went through a similar set of questions when I first went back to C, and I settled on simple init/deinit pairs to indicate that the internals of a struct require lifecycle management, because they allocate internally, or for invariants enforcement. Deinit is optional if there is no deallocation required.
Second, depending on need, a create/destroy pair which dynamically allocates the enclosing struct as a convenience for the caller. Mostly, the caller should be the one to decide where to put the struct, and therefore will own the lifetime. But in some cases, these pairs are helpful.
Of course the most important thing is naming consistency in your own code, rather than keeping a rigid standard. And predictable signals, such as “if there’s an init function I must use it, same for deinit.”