r/cprogramming • u/Anxious_Stretch9000 • Oct 22 '26
Should I deallocate memory?
I am currently writing a compiler.
The main places where I need to use dynamic allocation is ASTs and symbol tables. Once they were created, they will be needed until the program ends.
I'm currently using an arena allocator to as malloc for everything, because good manner is that you need to manually deallocate all heap memory, and it has added a lot of complexity to my program, since every function that potentially need to allocate memory is going to have an arena handler.
Should I delete all this stuff and let the memory leak (it is not going to run for a long time, once the program ends all the memory will be reclaimed by the operating system), or should I be a "good programmer" and keep using arena allocator?
24
u/TheFlamingLemon Oct 22 '26
If you want to you can hoard all the memory you allocate until the program ends, but don’t just allocate stuff and then lose the reference to it because the OS will take care of it. If you actually run into a memory issue that way then it will be impossible to debug without entirely rewriting your program
5
u/GoblinsGym Oct 22 '26
My compiler uses preallocated space and a simple bump allocator. Unless something is really temporary, I never return memory.
When parsing identifiers, I prepare the basic symbol structure and copy the name there (calculating the hash in the process). Then do the symbol lookup. If the identifier already exists (e.g. keyword), the symbol will be "forgotten" by not updating the allocation pointer. If it is new, it is added to the hash chains and the allocation pointer updated.
The operating system releases memory for me when execution ends.
Overallocation is not a big problem - the operating system will only assign physical memory when you actually touch a page of memory. If you do something as flamboyant as request a 1 GB buffer, the OS will prepare the page table for this space, and maybe give you a "huge" page for the first section.
6
u/NYU_VM Oct 22 '26
Your given info hardly shades any light into what kind of project you're working on(native compiled, bytecode compiled, Interpreted...), what's the pipeline and stages, etc. and thus it's requirements.
Arena allocator is a good start and correct choice for managing compiler memory. But Your compiler should not be needing AST's for the whole program, that's bad design, keep the dependency between stages minimal, it will let you test, scale and modify different compiler parts individually.
If you need AST for the whole program then is it a tree-walking interpreter? Or directly compiled from AST without much of semantics checking? Your post is really opaque to suggest anything.
So the general advice is to 1. Prefer arena allocation, bundle data with similar lifetimes and mass free them 2. Have a well defined data/control flow and Keep coupling between stages minimal, pass the necessary data and tables to receiver(the receiver becomes responsible for managing that memory)
3
u/NeuroSynchroBonding Oct 22 '26
If you really can't be bothered, there is always Boehm GC, which gives you an alternative function for allocating memory with automatic garbage collection.
2
u/Classic-Rate-5104 Oct 22 '26
Some complex pieces of code will require much memory and not freeing memory can result in out-of-memory problems which makes the code uncompilable. So be wise. Not freeing memory that's out of scope is just bad programming practice
2
u/zhivago Oct 22 '26
Yes, or you can't use it like a library.
And you should write it to be used like a library so you can test it.
But allocation is one of those things you should delegate to the caller.
Have them configure an allocator and make it not your problem.
Then you can use a leaky allocator in your program and they can do something else.
An interface like realloc should do the trick so you only need one function pointer.
1
u/am_Snowie Oct 22 '26
Compiler is a short lived program, it doesn't matter, iirc even gcc doesn't free memory, it just slows down the compilation i guess.
1
u/catbrane Oct 22 '26
I do both.
I have a
#define DEBUGmode where everything is freed carefully on exit (so I can run under valgrind and see a 0 at the end), and I have a release mode which just exits without the long cleanup to save a few ms on benchmarks.Doing both lets me include a valgrind run of my test suite during CI to check for any memory handling errors, and also be reasonably quick on benchmarks.
1
u/deckarep Oct 22 '26
If your app is a run once kind of thing, you don’t have to release memory at all because the OS reclaims it. It’s different for a long running game and worse for something like a server that be online for days or months at a time.
But, from a pure correctness standpoint, it doesn’t hurt to build the disciplined habit of always releasing memory precisely when you no longer need it. This is a good habit to get into and good to build the muscle memory behind it.
If you think of memory in terms of ownership and lifetimes you’ll have an easier time recognizing those checkpoints when you’d want to release memory.
So I would say to do it, and do it always as it doesn’t hurt and it’s a great habit to build.
1
u/Far-Championship-883 Oct 26 '26
use a arena bump allocator.
1
u/chibuku_chauya 29d ago
If you don’t deallocate at all you can gain speed but your compiler’s memory usage will balloon with large translation units.
1
u/Impossible_Box3898 Oct 22 '26
You’re doing wayyy to much premature optimization.
While arena allocation for ast nodes (and others) is ok, do you know if it’s even needed? Are you going to be constantly compiling multi thousand line files with your parser?
I would get the compiler working first and then optimize. You’re very likely to change fundamental things as you progress’s and dealing with optimization from the start can make that difficult.
Custom allocators are easy to add in afterwards if needed.
Personally I’d spend more time on things like string internment than ast optimizations.
The parsing stage tends to be far faster than later stages regardless.
1
u/DamienTheUnbeliever Oct 22 '26
If you're working in any modern OS, you're not allocating memory, you're allocating address space that is entirely a private matter for your process.
If you need that memory to stay allocated until your process exits then the cleanest thing to do is to not care about it. Your process is going to end, all of your address space allocations become irrelevant.
As Raymond Chen said, if the building is about to be demolished, you really shouldn't spend time emptying the bins.