Recent

Author Topic: alloca  (Read 4702 times)

MathMan

  • Sr. Member
  • ****
  • Posts: 408
Re: alloca
« Reply #45 on: March 03, 2023, 01:13:48 pm »
I once had another interesting use-case, where I needed to compute something by building a tree with a lot of nodes, where all the nodes where created dynamically, but the whole tree was freed at once. So what I did was I pre allocated a huge area of memory, on the heap, and then allocated the node objects on that like a stack. This way I had only one large heap allocation, while all the new allocations of the nodes where basically nothing more than incrementing a pointer on the stack. And after I was done doing the computation, all that was needed to remove the whole tree was to free that whole memory block as one.

While the time save per tree node was quite small, the total time save over billions of nodes was actually quite huge

Which is exactly the way I'm doing too, regularly. And tried to put this forward as an alternative in my initial post - but maybe I was too circumscribed, or too short, or too whatever there. You must know your environment in detail for this of course ...

 

TinyPortal © 2005-2018