Lably
Skip to content

Dead code

Computer code that is never executed

  • 3 min read
  • 2 chapters

Related on Lably

Through Computer programming, Exception handling and Method (computer programming)

While the result of a dead computation may never be used, it may raise exceptions or affect some global state, thus removal of such code may change the output of the program and introduce unintended bugs. Compiler optimizations are typically conservative in their approach to dead-code removal if there is any ambiguity as to whether removal of the dead code will affect the program output. The programmer may aid the compiler in this matter by making additional use of static and/or inline functions and enabling the use of link-time optimization.

Example

<span id="mwJg"></span><span class="kt" id="mwJw">int</span><span class="w" id="mwKA"> </span><span class="nf" id="mwKQ">foo</span><span class="p" id="mwKg">(</span><span class="kt" id="mwKw">int</span><span class="w" id="mwLA"> </span><span class="n" id="mwLQ">x</span><span class="p" id="mwLg">,</span><span class="w" id="mwLw"> </span><span class="kt" id="mwMA">int</span><span class="w" id="mwMQ"> </span><span class="n" id="mwMg">y</span><span class="p" id="mwMw">)</span><span class="w" id="mwNA"> </span><span class="p" id="mwNQ">{</span> <span class="w" id="mwNg"> </span><span class="kt" id="mwNw">int</span><span class="w" id="mwOA"> </span><span class="n" id="mwOQ">z</span><span class="w" id="mwOg"> </span><span class="o" id="mwOw">=</span><span class="w" id="mwPA"> </span><span class="n" id="mwPQ">x</span><span class="w" id="mwPg"> </span><span class="o" id="mwPw">/</span><span class="w" id="mwQA"> </span><span class="n" id="mwQQ">y</span><span class="p" id="mwQg">;</span> <span class="w" id="mwQw"> </span><span class="k" id="mwRA">return</span><span class="w" id="mwRQ"> </span><span class="n" id="mwRg">x</span><span class="w" id="mwRw"> </span><span class="o" id="mwSA">*</span><span class="w" id="mwSQ"> </span><span class="n" id="mwSg">y</span><span class="p" id="mwSw">;</span> <span class="p" id="mwTA">}</span>

In the C code above example, although the division of x by y is computed and never used, it will raise a SIGFPE (floating point exception signal) when a division-by-zero occurs. Therefore, the removal of the dead code may change the output of the program.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication