Note that there are some explanatory texts on larger screens.

plurals
  1. POI have a for loop that refuses to stay within the defined limits. The limits have been verified (by printing) during runtime
    primarykey
    data
    text
    <p>I am inserting the snippet of code that is causing the problem. Please focus on the uncommented code, as that is the actual code. The commented out code was there to help me debug. Apologies for poor readability, but I decided to let the comments stay, in case anyone had a doubt as to how I arrived at my diagnosis of the problem.</p> <pre><code>void backProp(double back_prop_err, double x[])// x is the complete output of input layer neurons. { //j++; //printf("\nA"); printf("\nj=%d",j); error = out*(1-out)*back_prop_err; printf("\nB:%20.18f",error); //if(j==8) //{ // printf("\nstart= %d, stop= %d",start, stop); // temp=1; //} for(int i=start; i&lt;= stop; i++) { // if(i==24) // break; // if(temp==1) // { // printf("\nstart= %d and stop= %d", start, stop); //temp=0; // } //j++; //printf("\nC"); del_w[i] = c*error*x[i]; printf("\ndel_w[%d]=%20.18f",i,del_w[i]); } } </code></pre> <p>Please ignore the commented out sections. They were there to display stuff on the screen, so I could debug the code.</p> <p>The Problem:</p> <p>There are 3 classes, let's call them A, B and C. A has 24 objects, stored as an array. B has 10 objects, again in an array and C has only 1 object.</p> <p>The above code is from the B class.</p> <p>for class B's object[7], the value of start and stop (see above code) is 0 and 23, respectively. I have verified the same during runtime using the commented out pieces of code that you see above.</p> <p>Yet, when the backProp function is called, at that particular for loop, an infinite loop is entered. The value of i keeps increasing without bound till DevC++ crashes. The commented out if(i==24) was put there to prevent that.</p> <p>This only happens with class B's object[7]... Not with the previous 7 objects (object[0]...object[6]). For them, that particular loop starts and stops as per the set "start" and "stop" variables. Is it important that for those objects, "stop" is a small number (like 6 or 12 max)?</p> <p>Class B's object[8] and object[9] also have stop = 23, which leads me to suspect that they too would face the same problem.</p> <p>The Question: Why is this happening? start and stop variables are set. Why isn't the loop staying within those limits?</p> <p>I have tried to keep this as concise as possible. Thank you for your efforts at reading this wall of a question.</p> <p>EDIT: start and stop are private variables of class B (of which, the above function is a public function). They are set to their respective values using another public function, which I call from main.</p> <p>j is somewhat embarrassing. It's a global static integer, initially set to 0. The reason I did this is so that I could count when class B's object[7] was accessing the backProp function. j gets incremented each time backProp is called, and as it's a static global, it acts as a counter so I can count till the object[7].</p> <p>As per Oak's suggestion, I am putting up the link for the code here: <a href="http://pastebin.com/ftxBGs2y" rel="nofollow">http://pastebin.com/ftxBGs2y</a></p> <p>Yeah, efforts are on to learn the DevC++ debugger. :P</p> <p>I am pretty sure I don't have any problems with the dimensions of x. I will look into the matter again though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI guarantee that such basic functionality as `for` is not broken. This means that your code is at fault, not `for`. In any case, this problem is best solved by using a debugger on your code - a real one, not print statements, such as the one in visual studio. Then you can look at the stack trace, all the values and see where they are coming from and investigate properly.
      singulars
    2. COIf you print `start` and `stop` and see good values, and then you get stuck in the loop, then something must have happened to these values after the point they were printed - for example, maybe another thread changed them (they are fields after all, not locals). If anything, try to print their values from *inside* the loop, or better yet, use a debugger to see their values during execution.
      singulars
    3. CO@Oak, I did do that. It prints 0 and 23 for start and stop outside the for loop and prints the same values from inside the for loop. The commented out section of the code inside the for loop does that. I only have Dev C++ right now, and am unable to understand how to use its debugger. Trying to get CodeBlocks of Visual Studio. I am afraid I do not understand fields and locals. Thank you for your assistance.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload