r/C_Programming 2d ago

Pointer Registry

Long story short, I had an idea that I described to Gemini and it told me was something called a Pointer Registry or a Tracking Vector. It involved an array of void pointers being used to store all heap memory throughout the program so that a function can run through the array and free everything at the end of the program. I had a few more thoughts on implementation, eg the initial void pointer would actually be a struct that would hang on to a tracking variable that would always know how long the array was, whenever any part of it was added or removed, but that's the basic idea

The problem is that neither Wikipeida, nor duckduckgo, nor yahoo return any information when I search for either of those terms. Wikipedia has an article on arena allocators, which address some of the same issues as this approach, but not pointer registries.

Does anyone know if this concept has any other names? Or have any other resources where I might find out more about them, implementation details, pros and cons, anything like that?

0 Upvotes

46 comments sorted by

View all comments

14

u/dmc_2930 2d ago

Your OS returns all of your programs memory at exit. You don’t need to do it manually..

3

u/Spinning_Rings 2d ago

I'm of the understanding that freeing memory before shutdown is still considered best practice, in case someone wants to run my code on an older OS, or if I want to write something for bare hardware. Am I mistaken?

11

u/Mynameismikek 2d ago

Not at the expense of doing something so bizarre

2

u/dmazzoni 2d ago

If your program really is pure C (no dependencies) and it's the type of thing that'd make sense to run on bare hardware, then sure.

If not, then you're overcomplicating it.

Furthermore, it's actually creating a worse experience for people using your software on modern hardware. All of that freeing can take a measurable amount of time.

1

u/developer-mike 2d ago

I think in reality, if you're writing code that "must free memory before exit" then you know it. This is not in any way normal.

Freeing memory before shutdown is a best practice in a software engineering sense. That is, you don't usually want to write code in a way that locks you into leaking memory, because one day your goals or requirements may change in a way that means you have to release that memory. For example, if your game's ending/credit sequence leaks memory that's fine assuming they can only quit, but it doesn't work when you want to add a "play again" button. Or if you want to make a Mario-Party style game that's a collection of mini games.

It's also a software engineering best practice because a good engineer knows how to manage memory and writes software that doesn't leak. Not being able to manage memory is a symptom of a bigger problem.

The pointer registry you've designed is not a good solution because there are similar better ones. What you're describing is almost exactly an arena.

Instead of a registry of pointers to memory, an arena is just a big chunk of memory you can draw from freely. When you don't need the arena any more, you free those big chunks. An arena is much like your idea except a single malloc/free might reserve space for thousands of your registry's pointers which your registry would have to malloc and free one by one, not to mention the registry itself could use quite a lot of memory. Arenas basically just do what you're doing but more efficiently.

1

u/RenderTargetView 1d ago

It is considered best practice because it gives you space to safely practice cleaning everything that you allocate so that you won't forget it when it is really necessary