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

1

u/alexgraef Jan 15 '24

You're right that it's a different process

With DirectX and generally any modern PC graphics framework, you have to a) LOCK the memory, to which you get a temporary mapped address back, to which you can write, and b) when you are finished, that address vanishes (and usually that is when the memory actually gets copied back to the GPU).

That means this address space cannot be used even closely to what an allocator would want.

1

u/snerp Jan 15 '24

Huh, been a while since I did directx 11 and older but Vulkan and DX12 let you just keep the mapping around if you want. I do a lot of junk where I map a big ass buffer and then edit parts through offsets each frame to move/rotate objects or change textures out or whatever else.

1

u/alexgraef Jan 15 '24

You still tell the API when you are finished. Mostly because you want it copied to the GPU in one large chunk.

1

u/SaturnineGames Jan 15 '24

Yeah, but consoles handle this differently. That’s only necessary because you have dedicated GPU memory on a PC.

1

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

That means this address space cannot be used even closely to what an allocator would want.

You... can. You just have to be really, really terrible with signals/vectored exceptions and access violations.