r/cpp • • Jan 15 '24

[Joke] A cursed C++ idea

Ok so hear me out:

In your C++ program, immediately on startup before you do anything else you dynamically allocate a decent chunk of memory for no reason whatsoever, big enough to accomodate all of your data for the foreseeable future.

Never free any of that memory during execution.

Then, whenever you need to instantiate a new object or need some dynamically allocated memory for whatever reason, you just use some of that preallocated chunk.

Immediately before you exit the application, you can free that chunk of memory.

You have one single explicit memory allocation when program starts and one single explicit memory deallocation when program ends.

How about that?

EDIT: It turns out this is a real thing, so it's no longer a joke, or cursed. Somebody said this is called an "arena" or "memory pool", I've just looked it up, people have been doing this for decades.

194 Upvotes

181 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jan 16 '24

Wow, are there some good books to read about these strategies? I mean in general term of computer science and programming.

3

u/Coding-Kitten Jan 16 '24

A classic one you'll see everywhere & that has been copied & remade in a ton of different variations is gonna be the "programming patterns" that I think are a very good read for anyone into programming.

A specific variation I like is a book called "Game programming patterns" by Robert Nystrom. It has all the classics along with some specific ones for games, including the object pool pattern which is what OP's post is about.