Is it possible to skip over an abitrary amount of a loop during debug?? Visual Studio
Asked Answered
S

5

10

I am trying to find an error in my code. The problem is the error occurs in a loop. But the loop iterates about 500 times. Instead of clicking through the loop. Is it possible to skip over a certain amount of the loop ??

Suwannee answered 24/5, 2010 at 23:7 Comment(0)
T
10

VS allows you to set a condition on a breakpoint in terms of variables that are in scope. So, in your case, you can test against the loop counter.

Trapezoid answered 24/5, 2010 at 23:8 Comment(1)
ok, I found it. Just right click on the break point and select hit countSuwannee
B
3

Here is a crude answer:

if ((iter % 10) == 0) {
    int stop = 1;
}

Then place a break-point at "int stop = 1;". Perhaps, there is a better way in VS but this is what I do from time-to-time.

Bind answered 24/5, 2010 at 23:10 Comment(3)
Quite. Just take the code out before you check in lest you make thedailywtf.Bussard
If you want to do that, surround that code with #ifdef DEBUG / #endif so that it's not part of your production build.Benzofuran
Conditional break-points can sometimes slow down your execution speed by a ridiculous amount. So if you need to skip millions or iterations instead of hundreds, this method can help preserve sanity.Motorway
O
1

You can assign new values to variables during debug session. Step through the loop statements as many times as you like, then set your loop counter (or whatever other vars maintain loop condition) to terminate the loop.

Ozuna answered 24/5, 2010 at 23:14 Comment(1)
I'm not sure this is what he's looking for but something every programmer should know anyway.Stratocracy
P
1

Yes. It's possible to skip exact loop counts with native VSCode debugger.

Steps:

  1. Right click on breakpoint (red dot) inside editor

enter image description here

  1. Click Edit Breakpoint...

Edit Breakpoint...

  1. Select Hit Count from dropdown menu

enter image description here

  1. Set number of loops to skip and press enter enter image description here enter image description here

  2. Run debugger with f5

Prober answered 9/1 at 10:16 Comment(2)
This is a good answer for VSCode, but the question was about Visual Studio (not Code).Tamikotamil
Good point there. I found this topic by searching exactly for 'VSCode', may considering this actual for today. Also possible dupes: #59016293 #76640714Prober
C
0

Just put the breakpoint in the loop like indicated below >>. Use F5 to get to the condition that causes failure so you can loop through the individual pass. How to know where to break is up to you.

for (int i = 0; i < LOOPMAX; i++) {
>>some_proc(i);
  some_other_proc(i);
  some_third_proc(i);
}

By pressing F5 it'll continue running till it gets to the next breakpoint (the next pass through the code). Sure you'll have to hit it 500 times, but that beats some thousands of times. Combine this with @Troubador code above.

PS: This answer IS really simple, but some people don't know they can do this.

Cupola answered 24/5, 2010 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.