I'd like to write my own memory manager. The target language is C++ and the goal of the memory manager is mainly to help debugging. It should detect double frees, memory overwrite and so on. And of course - I'd like to learn about memory management.
Can someone give me a hint so resources where I can learn how to write such a memory manager?
Thank you for your help.
What Is C++ Memory Management? Memory management can be defined as a process in which management of a computer's memory occurs, for example assigning memory to programs, variables etc., in such a way that it doesn't affect the overall performance.
Memory management is the process of controlling and coordinating a computer's main memory. It ensures that blocks of memory space are properly managed and allocated so the operating system (OS), applications and other running processes have the memory they need to carry out their operations.
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by a programmer. Dynamically allocated memory is allocated on Heap, and non-static and local variables get memory allocated on Stack (Refer to Memory Layout C Programs for details).
I think this is a very interesting project that you might learn a lot from. Here's a little bit of reading material on the subject of memory management. It goes over some of the basics of memory management, leads into a simple malloc implementation, then touches on a couple more advanced topics.
Inside memory management
Also, since you mention that you want to make a memory manager that is useful for debugging, you may want to consider reading this paper by the developers of Memcheck/Valgrind (A wonderful memory debugger for Linux). It details how they track all the the metadata (whether or not a particular byte is defined, intialized, etc.) in memchck. It's a bit detailed, but it's good reading on how to make a scalable and efficient dynamic memory checker.
How to Shadow Every Byte of Memory Used by a Program
Dave Hanson's C Interfaces and Implementations presents first a standard memory manager and then a memory manager with a few debugging features. This would be a great starting point to learn from and extend.
Of course, if you actually want to diagnose memory problems in a running C or C++ program, you should use valgrind
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With