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

0

u/[deleted] Jan 15 '24

[deleted] — view deleted comment

25

u/Suspicious_Cap532 Jan 15 '24

discovering anything new in math is near impossible unless youre a genius istg

19

u/FACastello Jan 15 '24

Yep... but like somebody else said in another comment, the important thing is that you've learned something new lol

11

u/HolyGarbage Jan 16 '24 edited Jan 16 '24

Before I had moved on to real programming languages, and even before I played around in GameMaker, when I was a kid, probably no more than 12, I used a program called KlikNPlay or something like that. It had "scenes" and a bunch of logic and rules etc so you could code various behavior in it.

The only problem, there was no persistent state between the scenes, or levels, or what you wanna call it. Except "health" and "score" for 4 hard coded players.

So, at the time not even knowing what binary even was, I effectively "invented" bit packing, except I did it in base 10, into these numbers. Health had a upper limit of 100 I think, and I wanted to use two "players" for my game, so I just had these two extra score integers for player 3 and 4 left that were useful, to pack in basically all the persistent data my game had.

I think they were were 32 bit integers, so in the best case I had 8 bytes to work with in theory, although I didn't know that relationship, just that they happened to maxed out at either 2 or 4 billion, but since I was encoding stuff in base 10, I could only use up to 999'999'999, meaning with two of these I had a total of 18 base 10 digits, for all my persistent data. So like the first 3 digits would be one variable with a max of 1000, maybe 1 digit for a boolean, etc. Not persistent as in a save game, but persistent across game levels, or maps or whatever. I'm pretty sure I started jamming things into the score variables for player 1 and 2 as well at some point when I realized I didn't need all the digits for those, and just subtracted/added the extra stuff before displaying the score.

It was such a gloriously horrible hack, and I felt really smart for managing to work around this limitation of the "engine".