Menu

Non-Zero for element beyond size of array?

Jarvis Loh
2008-07-30
2012-09-26
  • Jarvis Loh

    Jarvis Loh - 2008-07-30

    Hi!!

    I have a problem with the following, and hope that someone can help me out:

    TerPot[i][j]=cutoff[i][j]((Aexp(-lambda1rr))-(Bexp(-lambda2rr))b[i][j]); TerForce0[i][j][0]=-cutoff[i][j]((-Alambda1distcp[i][j][0](1/rr)exp(-lambda1rr))+(Blambda2distcp[i][j][0](1/rr)exp(-lambda2rr))b[i][j]); TerForce0[i][j][1]=-cutoff[i][j]((-Alambda1distcp[i][j][1](1/rr)exp(-lambda1rr))+(Blambda2distcp[i][j][1](1/rr)exp(-lambda2rr))b[i][j]); TerForce0[i][j][2]=-cutoff[i][j]((-Alambda1distcp[i][j][2](1/rr)exp(-lambda1rr))+(Blambda2distcp[i][j][2](1/rr)exp(-lambda2rr))b[i][j]);
    if (i>j) { // to overwrite the above and assign to a similar force
    TerPot[i][j]=TerPot[j][i];
    TerForce0[i][j][0]=TerForce0[j][i][0];
    TerForce0[i][j][1]=TerForce0[j][i][1];
    TerForce0[i][j][2]=TerForce0[j][i][2];
    }
    else if (i==j) {
    TerPot[i][j]=0;
    TerForce0[i][j][0]=0;
    TerForce0[i][j][1]=0;
    TerForce0[i][j][2]=0;
    }
    }
    }
    for (int i=0;i<N;i++) {
    for (int j=0;j<N;j++) {
    if (i==0 && j==16) {
    printf("TerPot[][] %.120f\n", TerPot[i][j]); // testing------------LINE #
    }
    }
    }

     printf(&quot;TerPot[0][1] %.120f\n&quot;, TerPot[0][1]); // testing-------------------LINE @
     printf(&quot;TerPot[0][2] %.120f\n&quot;, TerPot[0][2]); // testing
     printf(&quot;TerPot[0][3] %.120f\n&quot;, TerPot[0][3]); // testing
     printf(&quot;TerPot[0][4] %.120f\n&quot;, TerPot[0][4]); // testing
     printf(&quot;TerPot[0][5] %.120f\n&quot;, TerPot[0][5]); // testing
     printf(&quot;TerPot[0][6] %.120f\n&quot;, TerPot[0][6]); // testing
     printf(&quot;TerPot[0][7] %.120f\n&quot;, TerPot[0][7]); // testing
     printf(&quot;TerPot[0][8] %.120f\n&quot;, TerPot[0][8]); // testing
     printf(&quot;TerPot[0][9] %.120f\n&quot;, TerPot[0][9]); // testing
     printf(&quot;TerPot[0][10] %.120f\n&quot;, TerPot[0][10]); // testing
     printf(&quot;TerPot[0][11] %.120f\n&quot;, TerPot[0][11]); // testing
     printf(&quot;TerPot[0][12] %.120f\n&quot;, TerPot[0][12]); // testing
     printf(&quot;TerPot[0][13] %.120f\n&quot;, TerPot[0][13]); // testing
     printf(&quot;TerPot[0][14] %.120f\n&quot;, TerPot[0][14]); // testing
     printf(&quot;TerPot[0][15] %.120f\n&quot;, TerPot[0][15]); // testing
     printf(&quot;TerPot[0][16] %.120f\n&quot;, TerPot[0][16]); // testing
     printf(&quot;TerPot[0][17] %.120f\n&quot;, TerPot[0][17]); // testing
     printf(&quot;TerPot[0][26] %.120f\n&quot;, TerPot[0][26]); // testing
     printf(&quot;TerPot[0][27] %.120f\n&quot;, TerPot[0][27]); // testing          
     scanf(&quot;%d&quot;, nol); //testing
    

    }
    The above is only a part from my code.
    The problem is, I have only defined the size of TerPot[][] and TerForce0[][][] to be TerPot[N][N] and TerForce0[N][N][3], in which N is a constant fixed as 16 (const int N=16). But when i output TerPot[][] from LINE @ onwards, I get a non-zero value for TerPot[0][16]-infinity (not shown above).
    But in LINE #, no output is given, meaning it does not satisfy the condition. Why is this so?

    Thanks so much!

    Jarvis

     
    • Richard Kopcke

      Richard Kopcke - 2008-07-30

      PS if you display the addresses of your variables, you can find what locations array[i>15] might be addressing.

       
    • Richard Kopcke

      Richard Kopcke - 2008-07-30

      array [16] has allocated 16 storage locations 0,...,15. So addressing array[16] is a mistake.

       
    • Jarvis Loh

      Jarvis Loh - 2008-07-30

      Pls correct me if I am wrong, but I thought if i have defined 16 elements for a certain array and i want to output array[16] (or greater), the compiler will show me an error msg instead of running it and still giving me a non-zero value?

      Another thing I have found out is that for TerPot[i][j] and TerForce0[i][j][k] (as above), there are no problems with i. When I input i=16 and above, for eg. TerPot[16][0], it gives a zero. But when TerPot[0][16], there is a non-zero. Furthermore, I tried different values of j and it gives a non-zero only between 16 to 254. From 255 onwards, it is back to a zero value. Can you enlighten me on this? Is there anything to do with the range of int?

      Richard, you mentioned displaying the addresses of variables. Can you show me an example on how to do so?

      Thanks so much!

      Jarvis

       
      • Wayne Keen

        Wayne Keen - 2008-07-30

        "Pls correct me if I am wrong"

        You are wrong. One of the most common run-time errors is to read or write from or to an
        array out of bounds. C and C++ allows it, they do no array bounds checking. Many languages
        are the same, the only compiler I have used a lot that routinely did bounds checking was
        Ada.

        The out of bounds memory locations are loaded with anything - junk, zeros, even program code.
        There is no telling.

        Here is the kicker. You can get way with doing this - if the locations you are addressing
        do not have garbage that your program chokes on - or if the memory you are writing to isn't
        being used by someone else. But this changes, so you can get a lot of those - "But this
        works perfectly on compiler XYZ, there must be a bug in Dev-C++" statements that we get
        from time to time.

        Wayne

         
      • Richard Kopcke

        Richard Kopcke - 2008-07-30

        &array[0][0] or simply array[0] or simply array will produce the address of the first element of array.
        &array[15][15] will show the address of the last element of your 16x16 array.
        &array[15][16] will show the address of the next "storage location" beyond the last in the array.

        You can compare this last address to your map, specifically to the addresses of other variables, code, etc.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.