There are two types of errors in the C++ code that you may encounter:
(1) Errors that show up when you compile the code
(2) Errors that show up when you run the code (runtime error)
For error Type (1), it is relatively easy to deal with. The compiler usually tells you where the error is, and you could just check the lines that the compiler shows you. Most of the time, it’s a grammar issue.
But for error Type (2), it is tricky to deal with. I usually edit my C++ code using Notepad++, compile my C++ file using a command in the Linux environment, get the binary file, and then run the binary file using the ./BinaryFilename command. This method is fast and easy when there is no error in the code, however, when there is an error, the command window would just tell me the following:
Segmentation fault (core dumped)
And it doesn’t tell me where the error is.
To figure out where the error is, I usually do the following:
- I put the following line at the beginning of the code (right after int main { ):
int GoodCount = 0;
std::cout << "Good " << ++GoodCount << endl;
- After every 100 lines, I insert the following line (let’s call it the breakpoint line):
std::cout << "Good " << ++GoodCount << endl;
You could also insert the line every 200 lines, or whatever number of lines that you think is proper.
- Then I compile and run the code again. Let’s say I inserted 4 breakpoint lines in my code, and the command window is supposed to show me the following if the code runs well:
Good 1
Good 2
Good 3
Good 4
However, due to the error, it might turn out to be the following:
Good 1
Good 2
Segmentation fault (core dumped)
Then I know the error is between the 2nd and 3rd breakpoint lines.
- Then I insert another breakpoint line somewhere between the previous 2nd and 3rd breakpoint lines, compile and run the code, check whether the error is before or after the newly inserted breakpoint line. I just keep shrinking the interval until I figure out which line is causing the trouble.
Once you figure out where the error is:
According to my experience, most of the time, the runtime error is caused by problematic indices of arrays. In our code, we use a lot of loops and arrays. Sometimes we just didn’t use the right indices for the arrays, and the pointer is reaching part of the memory which doesn’t belong to the array that we intend to use. If you couldn’t figure out which index is causing trouble, you could add a line to print out the values of the indices before the line that is causing the error:
std::cout << "IndexName = " << IndexName << endl;
When you compile and run the code again, you will be able to see all the indices on the screen until the run-time error shows up, so you could check which index value is causing the problem.
Another note is, if you are getting a Gurobi exception at runtime, there might be other reasons, but please first check whether your Gurobi is properly installed and whether the environment variables are set properly.
There is also another additional help that could help you figure out what is causing the runtime error: How to catch the runtime error
If you have other tips about how to deal with runtime errors, please feel free to share them by replying to this topic.