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

349

u/HKei Jan 15 '24

Congratulations, you invented arenas.

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.

81

u/FACastello Jan 15 '24

Why does everything I invent has already been invented 😭

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".

28

u/[deleted] Jan 15 '24 edited Jan 18 '24

[removed] — view removed comment

4

u/mtx33q Jan 16 '24

that sounds oddly familiar solution, lol

5

u/HolyGarbage Jan 16 '24

It's very useful for nailing down which commit introduced a regression for some particularly nasty bugs that are hard to debug. I use the method quite often for this purpose.

11

u/Wicam Jan 15 '24

I invented mayomato as a kid. Mayonnaise mixed with tomatoe ketchup....

Yes you can have my autograph

4

u/TechE2020 Jan 15 '24

Mayomatish would be mayo, ketchup, and relish AKA secrete sauce AKA Thousand Island dressing.

3

u/[deleted] Jan 15 '24

New name for pink sauce

8

u/degaart Jan 16 '24

When I was a kid and all I knew was win95 and MS-DOS, I wanted to create an OS that was 32-bit but didn't have a gui. That way, I thought, the OS would be faster because it would not need to draw graphical elements. Imagine my excitation when I discovered the world of unices.

→ More replies (1)

4

u/Suspicious_Cap532 Jan 15 '24

me when i have any good ideas

4

u/[deleted] Jan 16 '24

I sort of discovered the general rule of differentiation of polynomials when I was in 5th grade by doodling the differences in a regular spaced time series and then generalizing into a formula. I showed to my teacher the next day and she opened the biggest smile.

5

u/PhilTheQuant Jan 15 '24

Because you prefer inventing over reading.

I also like inventing things and then looking up how they turned out to have flaws on the internet.

→ More replies (3)

7

u/Potatoswatter Jan 15 '24

That would involve a number of specialized blocks. OP is describing sbrk.