Hi - Is there a trick that you experts out there use or know of to test for unreachable code? Some code / function might be called by another piece of code, and so on....but what if that path is never taken? Therefore, it's not caught by the compiler as "unused"... I wish there was a way to search for "unexplored ". Any ideas?
Testing for unreachable code.
Collapse
X
-
Tags: None
-
Thi is done all the time in C#. The JTC (just-in time-compiler) can be set to predict which path you will most likely follow in order to anticipate which leg of code to compile. You might read a little on the design of this compiler in the C# books.
In C and C++, unreachable code is simply not compiled into your exectutable. Therefore, all code in the exectable is reachable. -
However it is possible to create code that should be reachable but isn't, for example failing to call a function or accidentally putting an early return in a function. You may still want to detect that unreachable code.
Most static analysis tools (PCLint for example or the one built into Eclipse) will inform you about code existing in your code-base that is unreachable.
On the other hand it sounds like you might be talking about code coverage. Unreachable code that is not logically unreachable so does exist in the code but where the conditions to to reach it are never met. A profiling tool can help out here it should be able to indicate the code coverage achieved in running your software and following that you may be able to find alternate run scenarios to achieve higher code coverage.
It is virtually impossible to achieve 100% code coverage, as a trivial example consider how would it be possible to force new to throw std::bad_alloc for a specific call while allowing all others to work? If you can do that then you almost certainly can't achieve full coverage on all your failure to allocate memory handling code.Comment
Comment