r/cprogramming • u/Ok-Maintenance-9790 • Jan 26 '26
A project that does a lot of dynamic memory allocation
I’ve been looking at C again recently because I got interested in OS architecture, and I want to get better at understanding memory and dynamic allocation.
For learning purposes, I’ve already implemented my own shell, worked with buffers, and tried simple allocator ideas like bump/arena allocation but I don't feel well in that topic yet.
I’ve seen people do things like re-implement OOP in C, but I wanted to focus on memory and processes.
What are some good things to implement in C if the goal is to learn more about memory management or allocator design?
This is purely for education.
Any ideas or suggestions are appreciated.
6
u/zhivago Jan 26 '26
Why not write a gabage collector?
8
u/siggystabs Jan 26 '26
Create a data structure.
For example I tried implementing a spatial partitioning scheme - a quadtree for a boids experiment. That took forever, taught me memory allocation and performance optimization.
7
u/PantsOnHead88 Jan 27 '26
Simple DB with flexible record sizes and contents.
Scan a dictionary into a tree and index it in several ways.
I don’t understand why you’re shooting down garbage collector. That might be one of the best possible projects for understanding dynamic memory management. C doesn’t natively have one? More reason to implement one!
2
u/ern0plus4 Jan 26 '26
Avoid.
Try to write programs which do not allocate any memory. In embedded world, sometimes it's mandatory.
Use slots, maximized unit sizes and numbers. Sometimes it can be exhausting, but the result is a program which never leaks.
2
u/mjmvideos Jan 26 '26
Make a simple web server
2
u/joel2001k Jan 26 '26
Look at double linked lists and sorted lists.
2
u/metroliker Jan 27 '26
Interpreted programming languages, especially simple ones, allocate like mad.
1
u/Key_River7180 Jan 26 '26
Make your own safe malloc? Like, try to keep track of memory, don't let the user override data, or leak memory.
1
u/TheTrueXenose Jan 27 '26
Memory allocator
1
u/kodifies Jan 27 '26
I'd recommend this - I've found it very useful just to double check I've not missed something...
for a project how about a doubly linked list, allocate the node structures dynamically (possibly later from a pool of pre-allocated memory) The node structure should have a void pointer to items you want to track. You could use it to do something like a simple physics sim where you are creating new items and deleting them as they leave the world area...
1
u/Business-Decision719 Jan 27 '26 edited Jan 27 '26
I say this only because you made it crystal clear within your post body that this is purely an educational exercise: Maybe start trying to implement a primitive lisp interpreter? Even a small one could be a seriously substantial project, and it'll make you think of how you'll handle storage for lists, symbols, syntax trees, parsing. People have already said make a GC, well trying to implement Lisp was how GC was invented in the first place.
But seriously, when I was just reading your post title, my knee-jerk reaction was "don't." Don't try to do a real world greenfield project that needs rampant heap usage in C in 2025. People who use C or the C-like subset C++ (especially no STL or exceptions) are often doing it specifically to limit heap allocations. If your projects job is just to shuffle blobs of high level data around the heap, and you're not using a memory safe language, then you're just torturing yourself at this point, and offering your software up to hackers on a silver platter. Admittedly, what you're asking about (how do I challenge myself to know how it works) is a good way to see why, and how other languages tried to fix this.
If it was in a serious codebase then it was because some people will just never use C++, or Objective-C/Swift, GObject/Vala, or whatever other examples of this being officially and purposefully done. Maybe the codebase turned out fine, maybe not. Most C code charges at this same windmill to one extent or another ("It'll be simpler and more performant if I roll my own in plain C!") and much of it just ends up even slower, uglier, more convoluted, and less maintainable than all that. There's an old programming joke that every mildly complex C program includes an inefficient half-baked implementation of Common Lisp. (Including Common Lisp.)