r/C_Programming • u/Spinning_Rings • Aug 07 '26
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?
21
u/smtp_pro Aug 07 '26
Question: what problem does that solve?
12
u/dmc_2930 Aug 07 '26
Your OS returns all of your programs memory at exit. You don’t need to do it manually..
4
u/QuirkyXoo Aug 07 '26
Memory must be freed by the code that allocated it once it’s no longer used. Anything else is very bad practice and if you’re unable to keep track of it, then your software is very badly designed.
And regarding the children’s story of “oh darling don’t worry, the OS will free it all when your program terminates”, just keep in mind that if allocations happen in loops or any cyclic way, and your software has to run for days, months, or indefinitely, your program will exhaust the system resources very quickly.
2
u/Helpful-Primary2427 Aug 07 '26
Sounds like an arena
6
u/This_Growth2898 Aug 07 '26
Why don't you ask the same Gemini to provide you with links?
1
u/ScallionSmooth5925 Aug 07 '26
That's a solution looking for a problem. Memory arenas are a similar concept if that helps. If for some reason you really whant to do this I would put the pointers in a linked list because it's easy to add elements to the front and later iterate on it. This way you don't have to store the length
3
u/manicakes1 Aug 07 '26
Doing multiple of these for different parts of an application is called Arena Allocation.
Common in systems where you need predictable memory management and/or no heap is available.
2
u/pedersenk Aug 07 '26
As a memory verification / debugging tool, possibly you may want to explore the concept of a 'Tombstone'
https://en.wikipedia.org/wiki/Tombstone_(programming)
It was a popular method for C on the classic Mac OS.
I have a type-safe (albeit violating strict aliasing) implementation here as libstent.
4
u/gizahnl Aug 07 '26
Freeing memory at the end of your program isn't important for you to do, the OS will do that.
And keeping a pointer to every allocation is going to wreak havoc to tools like valgrind making it impossible to debug memleaks.
Maybe you're confused with a memory pool? This is an allocation strategy where you allocate a big chunk of memory once, and sub allocate it when your program needs parts, saving you syscalls at the cost of having to deal with more complexity.
1
u/sciencekm Aug 07 '26 edited Aug 07 '26
This makes sense to do at the end of a function where you want to free all the memory that the function used, but it does not make sense to do at the end of a program because the OS will reclaim all memory once a program ends.
1
u/MyTinyHappyPlace Aug 07 '26
Your operating system provides blocks of free memory to your process. Once the process ends, those blocks are free to be re-used. There is absolutely no need to accommodate for any reasonable operating system of this millennium to free memory before the end of the process like that.
(I mean, it's good practice and all to take care of your allocated memory. But this is overkill.)
1
u/iOSCaleb Aug 07 '26
> free everything at the end of the program
Unnecessary. When the program exits, the OS reclaims its entire memory space.
In classic MacOS (before MaxOS X), it was possible (and common) to allocate relocatable blocks of memory. That allowed the OS to move blocks of memory around to reduce fragmentation. Relocatable blocks were accessed via handles, which were pointers to master pointers. When you allocated a relocatable block, the system would record the address of the block in a master pointer and return the address of that master pointer. Master pointers were kept in a list maintained by the system, not unlike your proposed list. The system worked reasonably well for the era and improved memory utilization at a time before virtual memory was common and RAM was measure in megabytes rather than gigabytes, but programmers had to be careful about how they accessed those relocatable blocks.
1
u/exo250 Aug 08 '26 edited Aug 08 '26
I made exactly what you described in my own framework (not publicly available yet). Don't listen those saying you don't need to free memory before exiting. Obviously it's not mandatory. But if you're a good programmer and making clean things, all resources should already have been freed, before calling your memory manager "free all" function which then will be able to report you any leak, without using a sanitizer.
Basically, I wrapped malloc, realloc and free. I have an internal registry that stores all the pointers. The fun part is that in debug build, for each pointer I also store the caller source file name + line. So I'm able to identify exactly the line of code of each non-freed/leaked memory allocation. I also thought adding the call stack and a simple string message to describe what is allocated (libwebsocket does this), but that's a bit overkill.
1
u/DueTouch8015 Aug 07 '26
Memory pool /Memory manage