Skip to content

quantcat26/memoryPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memory Pool Example

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.

Features

  • 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

Project Structure

.
├── main.cpp
├── Makefile
├── memoryPool/
│   ├── memoryPool.cpp
│   └── memoryPool.h
└── test/
    └── student.h

Build

Use make to compile the program:

make

Run

Run the sample program with:

make run

You can also run the compiled binary directly:

./main

How It Works

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

Notes

  • The code is written for C++11.
  • Student is only a sample type used to demonstrate allocation and deallocation behavior.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors