Most active commenters
  • jandrewrogers(3)

←back to thread

327 points AareyBaba | 21 comments | | HN request time: 0.001s | source | bottom
Show context
mwkaufma ◴[] No.46183728[source]
TL;DR

- no exceptions

- no recursion

- no malloc()/free() in the inner-loop

replies(9): >>46183820 #>>46183900 #>>46184073 #>>46184113 #>>46184198 #>>46184398 #>>46184472 #>>46184588 #>>46185500 #
jandrewrogers ◴[] No.46183820[source]
i.e. standard practice for every C++ code base I've ever worked on
replies(1): >>46183866 #
1. DashAnimal ◴[] No.46183866[source]
What industry do you work in? Modern RAII practices are pretty prevalent
replies(2): >>46183904 #>>46184092 #
2. jandrewrogers ◴[] No.46183904[source]
What does RAII have to do with any of the above?
replies(4): >>46183995 #>>46184069 #>>46184097 #>>46184150 #
3. WD-42 ◴[] No.46183995[source]
0 allocations after the program initializes.
replies(4): >>46184066 #>>46184067 #>>46184129 #>>46184331 #
4. nicoburns ◴[] No.46184066{3}[source]
RAII doesn't necessarily require allocation?
5. Gupie ◴[] No.46184067{3}[source]
Open a file in the constructor, close it in the destructor. RAII with 0 allocations.
replies(1): >>46187701 #
6. nmhancoc ◴[] No.46184069[source]
Not an expert but I’m pretty sure no exceptions means you can’t use significant parts of std algorithm or the std containers.

And if you’re using pooling I think RAII gets significantly trickier to do.

replies(1): >>46184330 #
7. Cyan488 ◴[] No.46184092[source]
This is common in embedded systems, where there is limited memory and no OS to run garbage collection.
replies(1): >>46187834 #
8. DashAnimal ◴[] No.46184097[source]
Well if you're using the standard library then you're not really paying attention to allocations and deallocations for one. For instance, the use of std::string. So I guess I'm wondering if you work in an industry that avoids std?
replies(1): >>46184756 #
9. tialaramex ◴[] No.46184129{3}[source]
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.

10. astrobe_ ◴[] No.46184150[source]
And what does "modern" has to do with it anyway.
11. theICEBeardk ◴[] No.46184330{3}[source]
https://en.cppreference.com/w/cpp/freestanding.html to see the parts you can use.
12. jjmarr ◴[] No.46184331{3}[source]
Stack "allocations" are basically free.
replies(1): >>46189939 #
13. jandrewrogers ◴[] No.46184756{3}[source]
I work in high-scale data infrastructure. It is common practice to do no memory allocation after bootstrap. Much of the standard library is still available despite this, though there are other reasons to not use the standard containers. For example, it is common to need containers that can be paged to storage across process boundaries.

C++ is designed to make this pretty easy.

14. dh2022 ◴[] No.46187701{4}[source]
std::vector<int> allocated and freed on the stack will allocate an array for its int’s on the heap…
replies(2): >>46189144 #>>46193524 #
15. criddell ◴[] No.46187834[source]
Garbage collection in C++?
16. usefulcat ◴[] No.46189144{5}[source]
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

replies(1): >>46189608 #
17. menaerus ◴[] No.46189608{6}[source]
WDYM? Vector is an abstraction over dynamically sized arrays so sure it does use heap to store its elements.
replies(2): >>46195886 #>>46205474 #
18. grougnax ◴[] No.46189939{4}[source]
No. And they're unsafe. Avoid them at all costs.
19. Gupie ◴[] No.46193524{5}[source]
Sure, but my point was that RAII doesn't need to involve the heap. Another example would be acquiring abd releasing a mutex.
20. aw1621107 ◴[] No.46195886{7}[source]
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.
21. ◴[] No.46205474{7}[source]