Hacker News new | past | comments | ask | show | jobs | submit login

The usage of single throw statement in the source code will result in more than 120KB of extra binary code in the final binary image. Just try it yourself with your compiler and see the difference in size of the produced binary images.

What?? 120KB? I have to try that out right away.

Edit: That turned out to be completely wrong. Simple program without throw: 71'218 bytes. With throw (one additional line that throws a plain integer): 71'814 bytes. GCC 4.8.1 on Windows. Would have been surprised if that was true.

Edit2: Okay, my bad, I should have linked statically. The statement sounded so absolute, but of course it is conditioned on the bare metal environment which is the subject of this article. Thanks for pointing out where I went wrong.




Using mingw-w64 (GCC 6.1.0):

  g++ -Os -static -s on `int main() { }' 17.408 bytes
  g++ -Os -static -s on `int main() { throw 42; }' 139.776 bytes


Which libc? You don't necessarily get as much bloat with embedded compilers and libraries.


Now what if you use two exceptions? If there's no significant increase, then the whole point is moot.


Not really moot. I do C++ on embedded processors with as little as 32kB of program space. Using a single exception means your code will not fit. My general rule of thumb to use full-fledged C++ is a processor with at least 256kB of program space.

Here's a comment of mine from a previous discussion: https://news.ycombinator.com/item?id=11706840


Not in the bare-metal environment. You may not have the memory to use one. In such a case, the increase to use two is irrelevant.


That's interesting. Have you linked dynamically? Maybe the author is talking about a statically linked binary, since he uses terms like "bare metal" and "final binary". Even if that takes an extra 120k, maybe additional throw statements won't be so significant.


The exception handling code in the GCC library is a lot more than 600 bytes. But if you were linking with the shared library, it won't be reflected in the size of the executable. Linking with the static library may show a quite different story.


Even when linked statically, my "Hello World" program is only 10,332 bytes larger than something which prints "Hello World" and then throws an integer.

Building the no exceptions version with -fno-exceptions doesn't change the size of the exception-free version either.

(g++ 4.8.5 with -Os)


All functions get compiled with exception handling unwind data, so you're going to get that pulled in whether the executable throws or not.

In order to not get that, you'll likely have to rebuild the entire runtime library with EH turned off.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: