Skip to content
Saarrthi Rana

School project / Winter 2025

Object pool

A C++ console program for my Object Oriented Design course.

Role
Solo
Team
Object Oriented Design course
When
Winter 2025
C++Object poolMemoryDesign patterns
Try it

Object pool demo

This runs the same steps as my C++ code. The balls are made once when the pool starts, and after that nothing is created or deleted. Click the board to take a ball out, and click a ball to put it back. Try running it empty, or putting back a ball that's already in the pool.

My original console version of the ball pool

Loading the pool

What it is

Creating and deleting objects every frame is slow, so games make them once and reuse them.

My pool makes five balls at the start, then hands them out and takes them back, so nothing gets created or deleted while it runs. A console menu lets you spawn a ball, return one, view the pool or reset it.

What I did
  1. Wrote the pool class. It makes all the balls up front and keeps one list of free balls and one of balls in use.
  2. Handing out a ball moves it from free to in use. If none are free, it tells you and doesn't make a new one.
  3. Returning a ball moves it back. Returning one twice, or one that doesn't exist, prints an error message instead of crashing.
  4. Wrote the Ball class and a coloured console menu.
  5. Drew the class diagram I handed in with it.
How it works

Two lists

All the balls are made at the start and deleted at the end. In between, they only move from one list to the other.

Why games do this

Bullets, sparks and enemies appear and disappear all the time. Creating and deleting each one takes time and fragments memory, so games make a batch once and keep reusing them.

Running out

When every ball is in use, the pool prints a message and returns nothing. It never makes extra balls, so its memory use doesn't grow.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

The pool

PoolClass.cpp, C++

Making the balls, handing them out and taking them back.
PoolClass::PoolClass(unsigned int num) : numberOfObjects(num) {
    for (unsigned int i = 0; i < numberOfObjects; ++i) {
        available.push_back(new Ball(i));
    }
    std::cout << GREEN << "Pool initialized with " << numberOfObjects << " Ball objects.\n" << RESET;
}

PoolClass::~PoolClass() {
    for (auto ball : available) delete ball;
    for (auto ball : inUse) delete ball;
}

Ball* PoolClass::GetBall(const vec& position) {
    if (available.empty()) {
        std::cout << RED << "No available balls in pool!\n" << RESET;
        return nullptr;
    }

    Ball* ball = available.back();
    available.pop_back();
    ball->Activate(position);
    inUse.push_back(ball);
    return ball;
}

void PoolClass::ReturnBall(unsigned int ballID) {
    Ball* ball = FindBallByID(ballID);
    if (!ball) {
        std::cout << RED << "Ball ID " << ballID << " not found in use." << RESET << std::endl;
        return;
    }

    ball->Deactivate();
    std::cout << GREEN << "Ball " << ballID << " returned to pool." << RESET << std::endl;
    inUse.erase(std::remove(inUse.begin(), inUse.end(), ball), inUse.end());
    available.push_back(ball);
}

void PoolClass::ResetPool() {
    while (!inUse.empty()) {
        ReturnBall(inUse.back()->GetID());
    }
}
Console menu of the ball pool showing free and in use balls
The original console version I handed in.

Written from scratch for the course.

Next school project

Super Mario Bros remake

A Unity remake of the first level of Super Mario Bros, made with Parth Sharma for a college assignment.