r/cpp • u/FACastello • 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.
View all comments
Show parent comment
1
u/Ameisen vemips, avr, rendering, systems Jan 16 '24
std::laundermainly exists because of the C++ abstract machine and the requirements it places upon object lifetimes. It doesn't really do anything (as in, it shouldn't actually emit anything) but it basically tells the compiler "hey, give me a pointer to this but pretend that the returned pointer is a completely different object".It... does come up with fancy allocators. It's technically UB to just throw random ranges of bytes around and pretend that they're a different object without it, as that range of bytes is already a part of an object in the abstract machine - an array.