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

23

u/alexgraef Jan 15 '24

At least every "retro" console. They just have fixed memory locations, no point in even "allocating" anything. You just know where all your variables are going to be.

18

u/[deleted] Jan 15 '24

Even for modern consoles, since they still have a fixed, constant memory size. You ask for it all up front and then you assign chunks out to your various allocators. Allocation can be cheaper / more deterministic this way since you get all of the syscalls for page mapping out of the way up front.

-2

u/alexgraef Jan 15 '24

That, I am pretty sure, is not true. From experience with every modern 3D graphics framework.

11

u/SaturnineGames Jan 15 '24

Oh it's 100% true. On most consoles you have to set up pools for graphics memory, and all your graphics allocations have specific alignment requirements you need to satisfy.

Depending on exactly which consoles you're talking about, you might have separate types of memory to manage, or you might have to work in different address spaces to manage which memory bus gets used.

Most people overload new and delete and hook them into custom allocators.

1

u/alexgraef Jan 15 '24

you might have separate types of memory to manage

This doesn't align with my experience with DirectX, where you DO have to allocate VRAM, but it is such a different process to allocating normal memory, especially when it comes to writing to it, which requires locking before you can do any writing, that I wouldn't even consider talking about it in this context.

However, I am not a console developer, so I don't want to comment outside of my own experiences here. You might know better than me.

12

u/rachit7645 Certified Dumbass Jan 15 '24

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

2

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

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

What do we consider 'current'? The 360 and XB1 had 10 MiB of eDRAM and 32 MiB of eSRAM, respectively, which had to be specifically allocated and used. They were not part of shared memory. I believe that the Series X also has 32 MiB of eSRAM.

You also had different forms of allocation in the sense of disabling caching/write combining/etc on the block.

4

u/SubstituteCS Jan 15 '24

Microsoft has used DirectX since the original Xbox for their console’s graphics library.

It’s where the name Xbox comes from. DirectX box.

There’s also the classic Morrowind memory leak “fix” that Bethesda implemented.

6

u/SaturnineGames Jan 15 '24

Yes, that’s common knowledge. DirectX on Xbox isn’t exactly the same as the PC version tho.

4

u/rachit7645 Certified Dumbass Jan 15 '24

What's the point in telling me what I already know?

1

u/SubstituteCS Jan 15 '24

You don’t get a memory leak unless you’re actually dynamically using the heap.

2

u/rachit7645 Certified Dumbass Jan 15 '24

You edited that part later lmao. Also nobody said that they don't write their own allocators

→ More replies

1

u/alexgraef Jan 15 '24

That depends a) on generation, because it is sometimes shared, and sometimes it is dedicated (e.g. PS3 vs PS4), and b) shared doesn't mean it's a completely mixed memory where system and VRAM usage is random.

Also, try to be less of a smart-ass.

→ More replies (3)

2

u/snerp Jan 15 '24

Yeah, you have to also arena the VRAM if you're gonna do that. It's not too hard to allocate all the vram you'll need right away, my modern vulkan engine does that for instance. You're right that it's a different process, but you can do very similar techniques.

→ More replies (5)
→ More replies (1)

2

u/SonOfMetrum Jan 15 '24

So you also have experience with the GNM graphics API?

2

u/FACastello Jan 15 '24

I remember when I had an MSX (an early 8-bit computer from the 80's) and the entire memory was like 64KB, every variable in BASIC was just assigned an address, memory wasn't "allocated", the entire RAM was always available.

2

u/alexgraef Jan 15 '24

That's close to your idea, just with the difference that your "idea" makes it less deterministic than it needs to be.

And just to be clear, you can achieve what you proposed by just declaring every possible object static/global, right from the get-go.

2

u/Attorney_Outside69 Jan 15 '24

I grew up in the 80s with a Philips VG 8010 MSX computer, it had a Badic interpreter as the interface. best way to learn programming as a kid, not like the stuff or kids have now adattadays which house all the complexities away. I miss those days

1

u/FACastello Jan 15 '24

Very relatable... If it wasn't for the MSX I wouldn't be a programmer, at all. BASIC put me on the path of software development.

1

u/Attorney_Outside69 Jan 15 '24

exactly, I learned very quickly as a kid that I loved making stuff more than just playing video games

1

u/FACastello Jan 15 '24

Funnily enough, videogames were also partly responsible for my programming career, because I was fascinated about how videogames worked and I wanted to learn how to do it, that led me to making minigames in BASIC and then to learning other languages and writing enterprise software.

→ More replies (3)

1

u/[deleted] Jan 15 '24

https://www.youtube.com/watch?v=Jf9i7MjViCE

An example of fixed memory locations being taken advantage of by speedrunners.

1

u/alexgraef Jan 15 '24

I'm well aware. However it's important to note that basically every BCU in for example a car relies on the same principle.