answersLogoWhite

0

The only way to assert unreachable code is to place the assertion, assert(0), within the suspected unreachable code itself.

However, just because the assertion never triggers it does not follow that the code is unreachable -- only that the condition(s) required to reach that code segment have not thus far arisen. How certain can you be that the condition will never arise?

Unreachable code can sometimes be detected at compile time, depending on the compiler. Simple errors such as placing a return statement before unreachable code can normally be detected this way. However, logic errors may not always be as obvious.

For instance,

if( !condition )

// do something.

else if( condition )

// do something else.

else

assert( false ); // unreachable...

In general, if there is no logical proof to say a code segment is unreachable, it's best to leave the segment in place. An assert( false ) will alert you when the segment is hit, but even if it is never hit, you cannot be certain it will never be hit without a logical proof. If the unreachable code is trivial then it is usually not worth the effort to prove it is unreachable. If it is non-trivial, the only issue is how you test it in the event it is actually reachable. In that case you must force the conditions that would make it reachable.

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is unreachable code in c plus plus?

Unreachable code is code that can never execute. It usually happens when you have a logic error in your code, such as when an if condition always evaluates true (or false). For example: int foo(int& x) { if( &x == NULL ) // always evaluates false... { // Unreachable code: std::err<<"int foo(int&) : argument int& is a NULL reference"<<std::endl; return(-1); } return(0); } In the above example, the entire if() statement is redundant because the argument, x, is a reference. In C++, references can never be NULL, so the if statement can never evaluate true. Furthermore, in this particular example, the entire function can be said to be redundant (but not unreachable), simply because it serves no purpose. It will always return zero.


What is a static block?

A static block is a code block defined with the 'static' keyword and is not inside others blocks. The static block is executed when the class is first loaded and the main purpose is perform all the initialization that may not be appropriate inside a constructor.


Which one executed first static block or static methods in java?

Static Blocks are always executed first. A static block is executed when your class is charged but a static method is executed only when is called, therefor first the class is charged and then is executed a method.


What is static code analysis used for?

The static code analysis is used for the analysis of computer software such as video games and programs like Microsoft Word or Photoshop. The difference between this and Dynamic Analysis is that this is performed without it actually executing/starting the program up.


What is static and dynamic memory?

A computer is built to contain two kinds of memory. There is static memory, which stays in the computer after it is powered off. Then there is dynamic memory, which is memory that is only accessible while the computer is on. The hard drive is an example of static memory, while RAM is an example of dynamic memory.

Related Questions

Whats tools can be used for static code analysis?

The tools that can be used for static code analysis are code analytics and Code Blocks. These two pieces of software can be used for static code analysis.


What is unreachable code in c plus plus?

Unreachable code is code that can never execute. It usually happens when you have a logic error in your code, such as when an if condition always evaluates true (or false). For example: int foo(int& x) { if( &x == NULL ) // always evaluates false... { // Unreachable code: std::err<<"int foo(int&) : argument int& is a NULL reference"<<std::endl; return(-1); } return(0); } In the above example, the entire if() statement is redundant because the argument, x, is a reference. In C++, references can never be NULL, so the if statement can never evaluate true. Furthermore, in this particular example, the entire function can be said to be redundant (but not unreachable), simply because it serves no purpose. It will always return zero.


What is cpt code 29130?

Application of finger splint; static


What are oklahoma lottery scratch off code?

The Oklahoma lottery scratch off code is not static, it is dynamic.


What is a static block?

A static block is a code block defined with the 'static' keyword and is not inside others blocks. The static block is executed when the class is first loaded and the main purpose is perform all the initialization that may not be appropriate inside a constructor.


What is the medical billing code 29130 used for?

Application of finger splint; static


Gate code for lock box in tyler tx?

The gate code for lock box in Tyler Texas is not static, it is dynamic.


Where I place the code?

I place the code on the line and get it


What is static equipment design?

Mechanical design of static equipment is checking the safety of the equipment as per standard code for given conditions.And its decide the sizing of the equipments.


How do you code and call a static method?

class MyClass { // Declare a static method to return the square of a number. public static int getSquare(final int n) { return n*n; } public static void main(String[] args) { // Call the static method to find 1522 System.out.println( MyClass.getSquare(152) ); } }


Which one executed first static block or static methods in java?

Static Blocks are always executed first. A static block is executed when your class is charged but a static method is executed only when is called, therefor first the class is charged and then is executed a method.


What is the routers command used to configure a static route for a router?

routing table code S