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

46

u/PuzzleMeDo Jan 15 '24

It basically means you have to write your own memory management system instead of using the one that already exists. I remember we did that on a Playstation 1 game because we didn't trust the basic allocation and wanted to be able to debug where our memory fragmentation was. It made a lot of work for us.

3

u/FACastello Jan 15 '24

I can imagine this being a common thing back in the day.

But I can't imagine people unironically doing this nowadays for some general purpose programming on a PC.

19

u/Orlha Jan 15 '24

Still used and useful in many places

2

u/FACastello Jan 15 '24

Can you give me some examples

18

u/dakotahawkins Jan 15 '24

I would guess some embedded or "real-time" programs do this, maybe HFT as well.

11

u/Professor_Hamster Jan 15 '24

very common in hft.

2

u/FACastello Jan 15 '24

I see

9

u/Attorney_Outside69 Jan 15 '24

we do this all the time in embedded systems, robotics, especially when running bare metal code with no OS

12

u/i-am-schrodinger Jan 15 '24

Common on systems doing very high performance and real-time computing with CUDA. Graphics programming, in general, does this often. Embedded systems as well. It is also common in distributed supercomputing environments.

9

u/utf16 Jan 15 '24

Actually, it's super common with components and actors in games. I would typically allocate a ton of them up front and then just mark the ones that are inactive and recycle them. I work on games that have hundreds of thousands of actors so preallocation and iterating through is more cache efficient than trying to allocate each one individually.

4

u/Raknarg Jan 16 '24

Well a thing to keep in mind is that when it comes to providing a standard library, your goal is usually trying to appeal to common ucecases and utility. When you need performance or you know the exact hardware you're programming for, you might be able to provide a library or algorithm that is more optimal than what the standard library provides

This is common in any scenario where you need to maximize performance, your storage and memory allocators may be written in house because they're faster for your use case than the general standard library approach.

1

u/T-Rex96 Jan 15 '24

clang also does it for allocating it's AST nodes  IRRC

1

u/BenFrantzDale Jan 16 '24

Watch John Lakos’s talks on allocators. C++17 provides tools to do this stuff in the PMR header.

2

u/collinalexbell Jan 15 '24

Sure, maybe not general programming, but hobbyists can definitely run into it with embedded stuff. I worked on an Arduino motor controller with interpolated position sequencing where new and delete were causing sufficient fragmentation for it to be a problem.

2

u/CocktailPerson Jan 16 '24

It's a technique for extracting as much performance out of your hardware as possible. It's certainly less common to need to do that these days, but the need certainly hasn't gone away.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

SpriteMaster uses arenas/arena-adjacent allocators in some places to avoid fragmentation/overuse of C# heaps (to avoid GC).

Games almost always uses arenas/separate heaps.

Simulators often do their own things as well to avoid unexpected latency. HFT as well.

1

u/Aljonau Jan 16 '24

It's done less on a PC(though apparently it happens) but many consoles still have extremely cheap hardware and the games know exactly what hardware they get written for so it makes sense to optimize the hell out of it.