What is the difference between a dead code and an unreachable code?

I thought those terms were synonymous, but a note in MISRA regarding dead code indicates that this is wrong? What's the difference? Is this a subset of the other?

+12
standards misra
source share
5 answers

Dead code - code that runs but is redundant, or the results have never been used, or add nothing to the rest of the program. Discards processor performance.

function(){ // dead code since it calculated but not saved or used anywhere a + b; } 

Unreachable code is code that will never be reached regardless of the logical stream. The difference is not fulfilled.

 function(){ return x; // unreachable since returned a = b + c; } 
+27
source share

Dead code

Code that performs functions that do not work. Basically, this will not affect what is deleted.

Inaccessible code

Code that, due to a different logic, will never be executed. This is usually a sign of error.

+4
source share

Inaccessible code

The code that the control flow enters is never during program execution. This is unreachable code - it is code that is never executed during program execution.

Dead code

A code that has no effect on the codes following it, regardless of how the control flow passes through the program. This is dead code - it is code that should not be executed during program execution or, in other terms, is useless.

So, in true terms, none of them is a subset of the other. But both unreachable code and dead code are usually removed by the compiler during the compilation process as part of the code optimization.

+3
source share

unreachable code is something that will never be executed because there is no flow control to achieve the code.

A dead code is what gets (or can get) execution, but its results are never used.

+1
source share

The following 2 examples in java explain the differences:

Example 1: Inaccessible Object

  public void f() { Object obj = new Object(); obj.a(); obj = null; //nulling out the pointer work(); // obj is unreachable } 

In Example 1, there is no longer a pointer to the object we selected, so the obj object becomes inaccessible.

Example 2: dead object

  public void g() { Object obj = new Object(); obj.a(); work(); // obj is dead } 

In the second example, since we are not passing obj as a parameter and obj is not used in work (), the obj object is useless because it is not used anywhere. Please note that it is still available as it has a link. Since this is achievable, it cannot be garbage collected. Example 2 will have a memory leak and memory loss, even in Java with GC!

0
source share

All Articles