3 ms·
The author gave the following suggestions for how to use manual memory management (MMM) languages more safely: - Don't use malloc. - For long-lived allocation
by chromatin 4y ago
The author gave the following suggestions for how to use manual memory management (MMM) languages more safely:
- Don't use malloc.
- For long-lived allocations, use per-type arrays.
- In a game, we might have an array for all Ships, an array for all Missiles, and an array for all Bases.
- For temporary allocations, one can also use an arena allocator.
- For temporary allocations whose pointers don't escape, one can also use the stack.
- All unions must be tagged 11 and treated as values.
- Always use bounds checking.
The author then gives a list of tools like ASAN to perform various analyses (static and dynamic) to help further increase safety.
What I did not see, however, was one of the simplest and most effective suggestions for MMM programs:
Never free.
That is, in batch type workloads, scientific computing, and many things that are
not a long-running server process, you may have the liberty and resources to allocate but never free, and this could also be an important tool in your chest.
- cb321 4y agoAlong these lines, there is also (from 1988!) the Boehm Garbage Collector just an -lboehm away on your link line which in your suggested mode can reclaim (most) of the memory you might think you would lose. [1] (C does have many gotchas, though.) [1] https://en.wikipedia.org/wiki/Boehm_garbage_collector https://en.wikipedia.org/wiki/Boehm_garbage_collector
- wrs 4y agoThe best way to do “never free” is suggestion #4, an arena allocator.