|
|
Subscribe / Log in / New account

C considered dangerous

C considered dangerous

Posted Sep 11, 2018 16:53 UTC (Tue) by DHR (guest, #81356)
Parent article: C considered dangerous

"An interesting side effect that came about while investigating this was a warning he got about unreachable code when he enabled the auto-initialization. There were two variables declared just after a switch (and outside of any case), where they would never be reached."

A declaration does not have to be reached unless it has an initializer (or is a VLA). So these would probably be correct C, and perhaps meaningful.

Any executable code between a switch head and the first case label (or other label) is unreachable and a good compiler should flag it. Adding an auto-initializer carelessly triggers this problem.

If a declaration in the switch body (and not further nested) has a scope covering the rest of the switch body. So all cases.

The unfortunate choice of -Wdeclaration-after-statement means that placing a declaration at the start of a switch body is sometimes a good choice.

A compiler should flag any unused variable.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds