Remix.run Logo
WD-42 a day ago

0 allocations after the program initializes.

tialaramex a day ago | parent | next [-]

RAII doesn't imply allocating.

My guess is that you're assuming all user defined types, and maybe even all non-trivial built-in types too, are boxed, meaning they're allocated on the heap when we create them.

That's not the case in C++ (the language in question here) and it's rarely the case in other modern languages because it has terrible performance qualities.

Gupie a day ago | parent | prev | next [-]

Open a file in the constructor, close it in the destructor. RAII with 0 allocations.

dh2022 a day ago | parent [-]

std::vector<int> allocated and freed on the stack will allocate an array for its int’s on the heap…

Gupie 10 hours ago | parent | next [-]

Sure, but my point was that RAII doesn't need to involve the heap. Another example would be acquiring abd releasing a mutex.

usefulcat 19 hours ago | parent | prev [-]

I've heard that MSVC does (did?) that, but if so that's an MSVC problem. gcc and clang don't do that.

https://godbolt.org/z/nasoWeq5M

menaerus 17 hours ago | parent [-]

WDYM? Vector is an abstraction over dynamically sized arrays so sure it does use heap to store its elements.

aw1621107 7 hours ago | parent [-]

I think usefulcat interpreted "std::vector<int> allocated and freed on the stack" as creating a default std::vector<int> and then destroying it without pushing elements to it. That's what their godbolt link shows, at least, though to be fair MSVC seems to match the described GCC/Clang behavior these days.

nicoburns a day ago | parent | prev | next [-]

RAII doesn't necessarily require allocation?

jjmarr a day ago | parent | prev [-]

Stack "allocations" are basically free.

grougnax 16 hours ago | parent [-]

No. And they're unsafe. Avoid them at all costs.