views:

129

answers:

6

Hi,

I am debugging a C code with Visual Studio. There is a loop called 10000 times and in one of the interactions, at the end of the loop, there is an error, as the program tries to access the N+1 value of an array of length N. I want to go back and debug the origin of the error and I wonder if somehow Visual Studio, in debugging mode, can highlight visually or tell me which lines of the source code were executed. Then it would be easier to find the error. Does anybody know if this is possible?

If this is not possible with VS, what other approaches could do this?

Thanks

A: 

I don't know of any such tool, but a substitute would be to print a distinct message within each conditional.

if(somethingThatMightNotHappen) {
    printf("This happened.\n");
    ...
}
David M.
but this is not practical if you have 10000 lines of code. If at least one could make it automatically ...
Werner
A: 

No, the VS Debugger doesn't allow you to move backwards through time.

However, have a look at conditional breakpoints. You could break when the loop is run the Nth (or N-1th or whatever) time or when a specific condition is met (like variable idx > 1000) and step through the code.

sbi
actually i think it does in vs2010; but i am not sure which languages it supports http://msdn.microsoft.com/en-us/magazine/ee336126.aspx
pm100
@pm100: Wow, interesting read! I hadn't had the time to play with VS2010 yet.
sbi
You can use GDB to do this as well with `record`, `rs` and `rn`.
mathepic
+3  A: 

put a conditional breakpoint where the array access is happening. That way your program will break on the N+1th access and you'll have the complete stack trace to work with.

Sridhar Iyer
I recommend every developer to spend half an hour to investigate the features of the debugger they use (such as putting a condition on a breakpoint as suggested by Sridhar Iyer). However, when I observe my colleagues, I see them adding print statements for debugging and wasting a lot of time. I just don't understand it...
Codo
+2  A: 

intellitrace in vs2010 can do this

http://msdn.microsoft.com/en-us/magazine/ee336126.aspx

pm100
A: 

If you set up code profiling in Visual Studio, you can configure it to provide code coverage information which will give some of the information you are looking for, but it won't help solve the problem.

The approach I would take would be to reduce the count of the loop to a manageable number (say 10) and step through the code. If you know what you expect to happen, I have found that stepping through code and verifying that it's performing the operations you expect for a small test set clarifies any strange behavior.

In my experience, nothing beats predicting what the computer will do and then verifying you both agree. The most common causes would be:

  • Your condition is LESS THAN OR EQUAL instead of LESS THAN
  • You test for one more than you expect
  • your array is one smaller than you expect
  • you start counting at 1 and loop through the number of elements

Off-by-one (the last three) is just about the most common mistake in algorithms.

Tom Slick