C matrix does funny things

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiddwaylll
    New Member
    • Jun 2008
    • 6

    C matrix does funny things

    I once had a matrix working like this

    matrix[20][2]

    So, since its indexed with 0, I used them as matrix[i][0] or matrix[i][1].

    Then the other day, I forgot the 0 index, and used matrix[i][2].

    And it worked perfectly fine! And worse so, the program didn't work anymore when I revert that back to matrix[i][1]. Also matrix[i][0] now had ridiculous values inside.

    More importantly, there are other places in the code in which the matrix works with matrix[i][0 or 1]. THE SAME MATRIX.

    Its just this particular segment that has gone haywire.

    I am using mingw latest version, and no the matrices are not passed by reference to anything or anywhere. And they are all initialized to all 0 values by a loop at the start of the code. The matrices are double type.

    My program is working fine, after about half an hour of randomly placing 0s, 1s and 2s somehow its working very fine. But I am afraid it would go bonkers again on another machine.

    Does anyone know whats happening? Is it my compiler or my coding?
    Thanks!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    All the indexes do is perform a pointer arithmetic calculation. If the indexes are inside your array you get good results. If they aren't, you get bad results since you have corrupted memory outside the array bounds.

    Read this article: http://bytes.com/forum/thread772412.html.

    Post again of your still have questions.

    Comment

    Working...