473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Testing for unreachable code.

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?
Nov 15 '11 #1
2 3178
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 16 '11 #2
Banfa
9,065 Expert Mod 8TB
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.
Nov 16 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bilgehan.Balban | last post by:
Hi, A simple question - Is it common/good practice to test C code using Python? For example one could wrap individual C functions, and test each of them using python, maybe not for low-level...
4
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct...
2
by: Hovik Melikyan | last post by:
This code produces a 'unreachable code' warning at line 16 (throw new X ...) with no visible reason: #include <string> class X { std::string msg;
6
by: dfetrow410 | last post by:
Can I do this in an if statement? public string getClass() { counta = counta + 1; if (counta < 2 ) {
4
by: Gernot Frisch | last post by:
I get informed that parts of my code cannot be reached. Did I do something wrong? try { if (login(progstring) > 0) { return 1; // OK } }
1
by: tam | last post by:
when i am using session variable i found one error that unreachable core error. how to rectify this error
3
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
2
by: Jakes | last post by:
Hey guys. Hope this is the right forum for this kind of question... I'm doing some vacation work and I have been given c++ code that has been written in Microsoft visual studio and I need to evaluate...
2
by: alireza6485 | last post by:
Hi, I am getting "Unreachable code dected " for my cariable n.Can you please let me know how to fix the problem.Thanks Console.WriteLine(); Console.Write("Decoded word :"); for...
1
by: DavidSS | last post by:
Okay, I have a problem. I just started programming, and made my first "calculator" I then started making it more advanced, so it wouldn't crash if you tried to divide by 0. Then I wanted to make...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.