Most active commenters
  • elteto(4)

←back to thread

327 points AareyBaba | 17 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 #
1. wiseowise ◴[] No.46183900[source]
That’s hardly 90% of C++.
replies(2): >>46183975 #>>46184047 #
2. bluGill ◴[] No.46183975[source]
Large parts of the standard library malloc/free.
replies(2): >>46183988 #>>46184076 #
3. canyp ◴[] No.46183988[source]
Or throw.
4. elteto ◴[] No.46184047[source]
If you compile with -fno-exceptions you just lost almost all of the STL.

You can compile with exceptions enabled, use the STL, but strictly enforce no allocations after initialization. It depends on how strict is the spec you are trying to hit.

replies(2): >>46184298 #>>46184314 #
5. gmueckl ◴[] No.46184076[source]
But you won't miss those parts much if all your memory is statically initialized at boot.
6. vodou ◴[] No.46184298[source]
Not my experience. I work with a -fno-exceptions codebase. Still quite a lot of std left. (Exceptions come with a surprisingly hefty binary size cost.)
replies(2): >>46184320 #>>46184674 #
7. theICEBeardk ◴[] No.46184314[source]
Are you aware of the Freestanding definition of STL? See here: https://en.cppreference.com/w/cpp/freestanding.html Large and useful parts of it are available if you run with a newer c++ standard.
replies(1): >>46184623 #
8. theICEBeardk ◴[] No.46184320{3}[source]
Apparently according to some ACCU and CPPCon talks by Khalil Estel this can be largely mitigated even in embedded lowering the size cost by orders of magnitude.
replies(2): >>46184444 #>>46184552 #
9. Espressosaurus ◴[] No.46184444{4}[source]
Yeah. I unfortunately moved to an APU where code size isn't an issue so I never got the chance to see how well that analysis translated to the work I do.

Provocative talk though, it upends one of the pillars of deeply embedded programming, at least from a size perspective.

10. vodou ◴[] No.46184552{4}[source]
Need to check it out. I guess you mean these:

- C++ Exceptions Reduce Firmware Code Size, ACCU [1]

- C++ Exceptions for Smaller Firmware, CppCon [2]

[1]: https://www.youtube.com/watch?v=BGmzMuSDt-Y

[2]: https://www.youtube.com/watch?v=bY2FlayomlE

11. elteto ◴[] No.46184623{3}[source]
Well, it's mostly type definitions and compiler stuff, like type_traits. Although I'm pleasantly surprised that std::tuple is fully supported. It looks like C++26 will bring in a lot more support for freestanding stuff.

No algorithms or containers, which to me is probably 90% of what is most heavily used of the STL.

12. elteto ◴[] No.46184674{3}[source]
Not exactly sure what your experience is, but if you work with in an -fno-exceptions codebase then you know that STL containers are not usable in that regime (with the exception of std::tuple it seems, see freestanding comment below). I would argue that the majority of use cases of the STL is for its containers.

So, what exact parts of the STL do you use in your code base? Most be mostly compile time stuff (types, type trait, etc).

replies(1): >>46185134 #
13. alchemio ◴[] No.46185134{4}[source]
You can use std containers in a no-exceptions environment. Just know that if an error occurs the program will terminate.
replies(2): >>46186937 #>>46188021 #
14. elteto ◴[] No.46186937{5}[source]
So you can’t use them then.
replies(2): >>46189176 #>>46203535 #
15. WD-42 ◴[] No.46188021{5}[source]
We've banned exceptions! If any occur, we just don't catch them.
16. _flux ◴[] No.46189176{6}[source]
I don't think it would be typical to depend on exception handling when dealing with boundary conditions with C++ containers.

I mean .at is great and all, but it's really for the benefit of eliminating undefined behavior and if the program just terminates then you've achieved this. I've seen decoders that just catch the std::out_of_range or even std::exception to handle the remaining bugs in the logic, though.

17. account42 ◴[] No.46203535{6}[source]
Of course you can, you just need to check your preconditions and limit sizes ahead of time - but you need to do that with exceptions too because modern operating systems overcommit instead of failing allocations and the OOM killer is not going to give you an exception to handle.