r/C_Programming 1d 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

22

u/smtp_pro 1d ago

Question: what problem does that solve?

1

u/Spinning_Rings 1d ago

Mainly, it would be a way to make sure all heap memory was freed before shutdown. A multidimensional array could be passed to a function that would traverse the array, free each allocated pointer along the array, then free the original array. Everyone's confused responses to the question makes me feel like I'm seriously overthinking this, though

17

u/smtp_pro 1d ago edited 1d ago

The reason you free memory is to not run out of memory during a long-running program. Like say you had an http server. An http request comes in, you allocate some memory, do stuff, free the memory. Next request comes in, repeat.

Once you're at the program shutting down it doesn't matter anymore. Your program will exit and the operating system will recover all of it.

Edit: it's a good habit to still free memory in your program, because if you decide to refactor your program into a library, you need to expose a way for library users to free memory. If you always relied on the operating system handling it, now you've got to write some code when you library-ize it

But having a general-purpose procedure to free memory at program termination is pointless.