This project demonstrates a simple generic memory pool in C++. It allocates objects from fixed-size blocks and reuses freed slots instead of calling new and delete for every object.
The sample program creates Student objects, destroys one of them, and then allocates another object to show that the freed memory can be reused.
- Generic
MemoryPool<T, BlockSize>implementation - Placement-new construction for objects with arguments
- Manual destruction and slot recycling
- Simple test program that prints object addresses to show reuse
.
├── main.cpp
├── Makefile
├── memoryPool/
│ ├── memoryPool.cpp
│ └── memoryPool.h
└── test/
└── student.h
Use make to compile the program:
makeRun the sample program with:
make runYou can also run the compiled binary directly:
./mainMemoryPool keeps a free list of reusable slots. When the pool runs out of free slots, it allocates a new block and links all of its slots into the free list. When an object is deleted, its memory is returned to the free list and can be used again by the next allocation.
In main.cpp, the program creates two Student objects, deletes one of them, and then creates a third student. The output shows that the third object reuses the memory released by the first one.
- The code is written for C++11.
Studentis only a sample type used to demonstrate allocation and deallocation behavior.