Function calling issues

Hi,

I have a code to solve for a physical process. U is declared as
 
Physical **U;

and is properly allocated. In the main code I do something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for (int i = 0; i<Ni; ++i){
    for (int j = 0; i<Nj; ++j){

      /*calculate U[i][j] */

      for (int n = 1; n <= 10; ++n) {
      bool isGoodState;
      /* perform some calculations */
      isGoodState = U[i][j].isPhysical(n);

        if(isGoodState) break;
         if(n==10){
         cout<<"going in "<<isGoodState<<endl;
         isGoodState = U[i][j].isPhysical(U[i-1][j],
                                          U[i+1][j],
                                          U[i][j+1],
                                          U[i][j-1]);
         cout<<"coming out "<<isGoodState<<endl;
        if(!isGoodState) cout<<"ERROR"<<endl;
      }
   } /* endfor */
 } 
}

After every iteration I do a check to see if the state is physical or not, using the following two functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool Physical::isPhysical(const int residual) {
             
     /* check if all the physical properties are satisfied 
        (compare some inequalities) */
       if (/*all inequalities are fine*/) return true;
       else return false;
  }


bool Physical::isPhysical(const Physical &U_left,
                          const Physical &U_right,
                          const Physical &U_top,
                          const Physical &U_bottom) {
      
    /* modify U[i][j] using the neighboring cells */
       
     /* check if all the physical properties are satisfied 
       (compare some inequalities ) */
       if (/*all inequalities are fine*/) return true;
       else return false;
  }

When I run this code I get an error with the following output.
1
2
3
going in 0
coming out 0
ERROR

But to debug it, if I introduce any cout statement inside isPhysical
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Physical::isPhysical(const Physical &U_left,
                          const Physical &U_right,
                          const Physical &U_top,
                          const Physical &U_bottom) {

     cout<<"inside isPhysical \n";      
    /* modify U[i][j] using the neighboring cells */
       
     /* check if all the physical properties are satisified 
       (compare some inequalities ) */
       if (/*all inequalities are fine*/) return true;
       else return false;
  }

, the code starts to run fine, with the output as:
1
2
3
going in 0
Inside isPhysical
coming out 1

Any ideas what is going wrong?
Thanks
if I introduce any cout statement inside isPhysical, the code starts to run fine


Smells like heap corruption. You're probably stepping outside of your array bounds somewhere.

Note the problem could be anywhere in the program, not just in Physical. Heap corruption is a beast.
Any suggestions how to track/debug it?
Use an std::vector instead of an array, and use .at() instead of the [] operator. .at() throws an exception when you go out of bounds (or maybe it was an assert()?)
Or use something like valgrind or efence, but firedraco's suggestion is probably a best first step.
Topic archived. No new replies allowed.