ArticleslgStudy

computer science

Unreachable code

Unreachable code is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Unreachable code rather than just read about it. In short: In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program. Unreachable code is sometimes also called dead code, although dead code may also refer to code that is executed but has no effect on the output of a program.

Key takeaways

  • Unreachable code belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Unreachable code to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Unreachable code from memory before moving on to harder problems.

Reference excerpt

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program. Unreachable code is sometimes also called dead code, although dead code may also refer to code that is executed but has no effect on the output of a program. Unreachable code is generally considered undesirable for several reasons:

It uses memory unnecessarily It can cause unnecessary use of the CPU's instruction cache This can also decrease data locality Time and effort may be spent testing, maintaining and documenting code which is never used Sometimes an automated test is the only thing using the code. Unreachable code can have some legitimate uses, like providing a library of functions for calling or jumping to manually via a debugger while the program is halted after a breakpoint. This is particularly useful for examining and pretty-printing the internal state of the program. It may make sense to have such code in the shipped product, so that a developer can attach a debugger to a client's running instance.

Causes Unreachable code can exist for many reasons, such as:

programming errors in complex conditional branches a consequence of the internal transformations performed by an optimizing compiler; incomplete testing of new or modified code Legacy code Code superseded by another implementation Unreachable code that a programmer decided not to delete because it is mingled with reachable code Potentially reachable code that current use cases never need Dormant code that is kept intentionally in case it is needed later Code used only for debugging. Legacy code is that which was once useful but is no longer used or required. But unreachable code may also be part of a complex library, module or routine where it is useful to others or under conditions which are not met in a particular scenario. An example of such a conditionally unreachable code may be the implementation of a general string formatting function in a compiler's runtime library, which contains complex code to process all possible arguments, of which only a small subset is actually used. Compilers will typically not be able to remove the unused code sections at compile time, as the behavior is largely determined by the values of arguments at run time.

Examples In this fragment of C code:

the definition int z = x + y; is never reached as the function always returns before it. Therefore, variable z neither needs to be allocated storage nor initialized.

goto fail bug Apple's SSL/TLS from February 2014 contained a major security flaw known formally as CVE-2014-1266 and informally as the "goto fail bug". The relevant code fragment is:

Here, there are two successive goto fail; statements. In the syntax of the C language, only the first statement after an un-braced if statement is conditional. The second goto fail; is therefore unconditional, and hence always skips the call to SSLHashSHA1.final. As a consequence, err will hold the status of the SHA1 update operation, and as long as both calls to SSLHashSHA1.update succeed, signature verification will never fail. Here, the unreachable code is the call to the final function. Applying the Clang compiler with the option -Weverything includes unreachable code analysis, which would trigger an alarm for this code.

C/C++ In C and in C++, some constructs are specified to have undefined behavior. A compiler is free to implement any behavior or none, and typically an optimizing compiler will assume the code is unreachable.

Analysis Detection of unreachable code is a form of control-flow analysis to find code that can never be reached in any possible program state. In some languages (e.g. Java) some forms of unreachable code are explicitly disallowed. The optimization that removes unreachable code is known as dead code elimination. Code may become unreachable as a consequence of transformations performed by an optimizing compiler (e.g., common subexpression elimination). In practice the sophistication of the analysis has a significant impact on the amount of unreachable code that is detected. For example, constant folding and simple flow analysis shows that the inside of the if-statement in the following code is unreachable:

However, a great deal more sophistication is needed to work out that the corresponding block is unreachable in the following code:

Unreachable code elimination technique is in the same class of optimizations as dead code elimination and redundant code elimination. Some languages allow explicitly marking code as unreachable:

C: via the unreachable() macro (in <stddef.h>) C++: via the std::unreachable() function (in <utility>), which is [[noreturn]] C#: can be indicated using the System.Diagnostics.Debug class, using the Debug.Fail() method Java: usually marked by throwing the java.lang.AssertionError exception Rust: via the unreachable!() macro Swift: via fatalError(), or functions returning Never Zig: via the unreachable keyword

Unreachability vs. profiling In some cases, a practical approach may be a combination of simple unreachability criteria and use of a profiler to handle the more complex cases. Profiling in general can not prove anything about the unreachability of a piece of code, but may be a good heuristic for finding potentially unreachable code. Once a suspect piece of code is found, other methods, such as a more powerful code analysis tool, or even analysis by hand, could be used to decide whether the code is truly unreachable.

See also Code coverage Redundant code Dead code Oxbow code Halting problem – the general problem of determining whether a piece of code is unreachable is at least as hard as the halting problem and hence undecidable

References

Appel, A. W. 1998 Modern Compiler Implementation in Java. Cambridge University Press. Muchnick S. S. 1997 Advanced Compiler Design and Implementation. Morgan Kaufmann.

Worked examples

Example 1 — a first encounter with Unreachable code

Start with the simplest possible case. Write down what Unreachable code claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Unreachable code before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Unreachable code ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Unreachable code

In research
Unreachable code appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Unreachable code in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Unreachable code is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler optimizations, Software anomalies, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Unreachable code outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Unreachable code” →

How to study Unreachable code in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Unreachable code means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Unreachable code out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Unreachable code in simple terms?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program. Unreachable code is sometimes also called dead code, although dead code may also refer to code that is ex…

Why does Unreachable code matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Unreachable code?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Unreachable code.

Tags

  • Compiler optimizations
  • Software anomalies
  • Source code

Keep exploring