3 ms·
Open a file in the constructor, close it in the destructor. RAII with 0 allocations.
by Gupie 10mo ago
Open a file in the constructor, close it in the destructor. RAII with 0 allocations.
- dh2022 10mo agostd::vector<int> allocated and freed on the stack will allocate an array for its int’s on the heap…
- usefulcat 10mo agoI'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 https://godbolt.org/z/nasoWeq5M
- menaerus 10mo agoWDYM? Vector is an abstraction over dynamically sized arrays so sure it does use heap to store its elements.
- aw1621107 10mo agoI 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.
- deleted 10mo ago[deleted]
- Gupie 10mo agoSure, but my point was that RAII doesn't need to involve the heap. Another example would be acquiring abd releasing a mutex.