How can i skip over n iterations using GDB? I'm trying to debug a for loop and i want to get to iteration 703, without typing next 703 times.
Attention: I want to remain in the loop, only to skip n iterations.
Thank you!
I found the solution:
Considering this code:
for(int i = 0; i < 1000; i++) {
printf("%d\n",i);
}
In GDB you have to type: 'break n' ( where n is the line number for the 'for' loop) and then 'c x' - where x is the number of times you want to skip the loop.
Try a breakpoint count: https://sourceware.org/gdb/onlinedocs/gdb/Conditions.html
(gdb) ignore bnum count
Set the ignore count of breakpoint number bnum to count. The next count times the breakpoint is reached, your program’s execution does not stop; other than to decrement the ignore count, GDB takes no action.
Related
I don't understand why I get x with a value of 6 while I think it should be 5.
Sub Main()
Dim x = 0
For x = 1 To 5
Next
Console.WriteLine(x)
Console.ReadLine()
End Sub
Result: 6
The reason that x equals 6 is because of the nature of a loop. You put no code inside the body of the loop. If you printed your code there you would see
1
2
3
4
5
Each time Next is reached, x is incremented. The fifth time you go through the loop, x is incremented to 6. In most cases it's best to not use loop variables outside of their loop. Using a C style loop what I mean is a bit more clear
for (int i=0; i<=5; i++){}
The loop runs until the condition i <= 5 is not true. Since each time through the loop i is increased by 1 this occurs first when i equals 6. I used the variable i here because i is a much more common loop variable name to see than x.
Does anyone know if the following can be an example of firstprivate in openmp?
rowstr[0] = 0;
for (j = 1; j < nrows+1; j++) {
rowstr[j] = rowstr[j] + rowstr[j-1];
}
nza = rowstr[nrows] - 1;
firstprivate variable is rowstr and j is a private variable.
Actually not, if you use the firstprivate clause you may have inconsistency in your output as some values would never be updated, clarifying:
Let's suppose it's an array of size 4 and you have 2 threads, one thread you get iterations 0 and 1 and the other 2 and 3 (in a perfect world). If you use the firstprivate clause the second thread will sum the position 2 of the array with was initially in the array in position 1, instead of summing it with the previous iteration as the sequential version would do.
Not just that, this particular loop have dependency issues and you should use something like a sum reduction in nza.
What does it mean in Go that the pre and post statements of a for loop are empty, like in the following example?
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
Remember that a for loop is the same as a while loop.
Your code can be rewritten in other languages as
sum := 1
while(sum < 10) {
sum += sum
}
fmt.Println(sum)
In a for loop, there are 3 parts.
for(initial statement ; condition ; end statement usually iterate)
This is equivalent to
initial statement
while(condition) {
Stuff here
End iteration statement
}
The reason your loop can be written withiut the pre and post statements is because you've specified them in other parts of the code.
It behaves like a while in other languages. You don't need the two semicolons:
sum := 1
for sum < 10 {
sum += sum
}
fmt.Println(sum)
A for loop has three elements: initialization statement, condition check, and variable change.
for <initialization statement>; <condition check>; <variable change>{
<actual body>
}
The initialization statement is executed only once when the loop starts. Based on the name, it initializes something (in a lot of cases a variable you iterate through). If it is omitted, then it does nothing
The condition check verifies whether the condition evaluates to true. If it is not, the loop stops. If it is omitted, then it is always true.
The variable change is modifying variables during each iteration of the loop. Most of the time, the iterated variable is increased/decreased, but you can do whatever you want. If it is omitted, it does nothing
After this explanation, you can see that this loop does nothing during your initialization and post condition phase.
You also do not need to use semicolons here. This will be enough.
sum := 1
for sum < 10 {
sum += sum
}
You can even write a loop like this: for {} which will never stop executing, or do something like a while loop:
t := 10
for t > 0{
t--
}
Note that inside your initialization, condition, and change phase you can use many expressions (not just one). So with a simple while loop you can do something like:
for f1, f2, n := 1, 1, 10; n > 0; f1, f2, n = f2, f1 + f2, n - 1{
fmt.Println(f1)
}
which creates Fibonacci numbers—see Go Playground. Showing this not because this is the best way to write it, but rather because it is possible.
I'm trying to decrement the counter of a for loop as the loop is running. Unfortunately, Lua doesn't seem to allow that. This piece of code should run forever:
for i = 1, 100 do
print (i)
i = i - 1
end
but it does, in fact, simply print the series 1-100. Is that by design? If so, how do I decrement the counter of a running loop (for example because the current cycle was disqualified and should run again)?
It's by design. From Lua reference manual:
3.3.5 – For Statement
All three control expressions are evaluated only once, before the loop starts. They must all result in numbers.
So modifying the value of i inside the loop won't change how the loop runs.
for i = 10, 1, -1 do
print(i)
end
If you want to step backwards through a table then do:
for i = #SomeTable, 1, -1 do
print(SomeTable[i].someproperty)
end
Yu Hao above linked to the correct manual page, but quoted the wrong part of it.
Here is the correct quote
for v = e1, e2, e3 do block end
is equivalent to the code:
do
local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
if not (var and limit and step) then error() end
while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
local v = var
block
var = var + step
end
end
[..]
var, limit, and step are invisible variables. The names shown here are for explanatory purposes only.
In other words, the variable that's being looped over (called "var" above) and the variable exposed to the developer (called "v" above) are different. There is no way to access the former.
The next Matab code, I need to keep the number of the last iteration:
A, B, arrays N numbers, increasing linearly.
for i 1:1:10
if A(i) < B(i) && A(i+1) > B(i+1)
number = i
end
end
disp(i)
Unfortunately this code is not working.
I need to find and keep the number i, in which the relation A and B is changing.
any help is more than welcome
Is this what you're trying to do?
A=rand(20,1);
B=rand(20,1);
for i=1:1:10
if A(i) < B(i) && A(i+1) > B(i+1)
number = i;
break; % Did you intend to stop when condition was satified?
end
end
% Presumably you wanted to display the stored index
% (although since we now break i and number will be the same)
disp(number)
BTW, Best to post code that can be run in your question. Makes it easier for people answering to see the problem.