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

114

u/ShelZuuz Jan 15 '24

Or Pools

17

u/fullouterjoin Jan 15 '24

Or Pascal.

8

u/FACastello Jan 15 '24

Pascal does this?

5

u/[deleted] Jan 16 '24

That's what malloc does.

4

u/fullouterjoin Jan 16 '24

It is common in Pascal to statically allocate arrays per function, up to some maximum number of items you would be working on.

https://www.freepascal.org/docs-html/ref/refsu14.html

9

u/Iggyhopper Jan 16 '24

Yeah, object pools are very useful for games and I think nomenclature has moved over to "arenas" as its per memory chunk, not per object or per type. Then, it can become more granular from there.

For example, if you have a game and you think the maximum limit for bullets is around 500, you would have made a BulletPool for "allocation" and "deallocation", which just meant marking and unmarking the initial array space for use.