I would agree, that it gives you more visibility, but the only flexibility I can think of, is that you might decide to not free the allocated memory.
So I would say it's a tradeof between "more typing" and "more visibility", that you have to decide yourself :)
If you are not the only one working on the codebase, you should probably stick to the "classical" approach, but I don't know.
Also sorry for being the "stack overflow guy" and suggesting option three of two, but I think this is one of the problems that linear allocators / arena allocators solve very cleanly.
1
2
3
4
5
6
7
8
9
10
11
12
13 | void do_some_rendering_or_something(memory_arena *scratch){
// save the state of the allocator so we can rewind to this point
temporary_memory temp = begin_tempoary_memory(scratch);
// do a bunch of allocations, might call functions and whatever
for(int i = 0; i < 100; i++){
Animation animation = push_animation(scratch);
// ...
}
// and they are all gone
end_tempoary_memory(temp);
}
|
So instead of thinking about memory ownership, which I always find confusing, you purly think about the lifetime of the allocations.
Here you might also do defer{ end_tempoary_memory(temp); } or have have a destructor for tempoary_memory, so you can early out.
If you don't know the articles by gingerBill about memory allocation I think they are a good read :)
link.