Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write your own memory manager [closed]

Tags:

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.

like image 882
Tobias Langner Avatar asked Jul 28 '09 14:07

Tobias Langner


People also ask

What is memory management in C++?

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.

What is the purpose of the memory manager in an operating system?

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.

How does memory allocation happen in C++?

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).


2 Answers

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

like image 165
Falaina Avatar answered Sep 20 '22 14:09

Falaina


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.

like image 34
Norman Ramsey Avatar answered Sep 17 '22 14:09

Norman Ramsey