Like Tree1Likes
  • 1 Post By TheBigH

strange matrix error

This is a discussion on strange matrix error within the C Programming forums, part of the General Programming Boards category; so I have a program that reads a netlist and creates a temporary matrix which has organizes the nets as ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    strange matrix error

    so I have a program that reads a netlist and creates a temporary matrix which has organizes the nets as rows and the nodes as columns.
    It then looks at the number of nodes connected to each net by comparing each term to one another in the each row.
    The program executes correctly up until usually 3-4 rows before the end of the matrix, then goes into a random loop comparing garbage values that are not contained in the matrix and crashes.
    I've been troubleshooting for a while now and tried a few different netlist inputs, varying in size from 49-65 rows and it always works right up until 3-4 rows before the finish, so I am hoping anyone can help identify the issue in the loop

    Code:
    for(i=0;i<=nets-1;i++)
        {
            fscanf(nlist,"%s %d %s",string,&integer,string+3);
            fprintf(olist,"\n%s %d %s",string, integer,string+3);
            fscanf(nlist,"%d",&integer);
            tmpmatrix[i][0]=integer;
            printf("\n%d ",tmpmatrix[i][0]);
            fprintf(olist,"%d ",integer);
            for(k=1;k <=nodes-1;k++)
            {
                fscanf(nlist,"%d",&integer);            
                if (integer != tmpmatrix[i][k-1])
                {
                    tmpmatrix[i][k]=integer;
                    printf("%d ",tmpmatrix[i][k]);
                    fprintf(olist,"%d ",integer);
                    length[i]++;
                }
                else break;
            }
        }
        fclose(olist);
        fclose(nlist);
            
        for(i=0;i<=nets-1;i++)
        {
            for(j=0;j<=length[i]-1;j++)
            {
                printf("\n");
                for(k=j+1;k<=length[i];k++)
                {
                    if (tmpmatrix[i][j] < tmpmatrix[i][k] && tmpmatrix[i][k] < nodes)
                        printf("%dchange %d<%d ",i,tmpmatrix[i][j],tmpmatrix[i][k]);
                }    
            }
        }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem most likely is in your for() loops... you are using <= as your loop condition and that usually means you are counting 1 too far...

    For example... when you create an array of 5 elements... int array[5];... you get valid array index values of 0, 1, 2, 3 and 4 ... count them, that's 5 elements.... array[5] takes you into memory your array does not own.

    So....
    Code:
    int array[5];
    int i;
    
    // correct
    for (i = 0; i < 5; i++)
      array[i] = 10;
    
    // not correct
    for (i = 0; i <= 5; i++)
       array[i] = 10;

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Thanks for the quick reply and attempt at help, unfortunately the error was not anything as simple as that- clearly it was not off just by one value, and using a less than or equal to operator is really not that difficult to keep track of.
    I actually discovered on my own that the length[i] array was putting garbage values in the last three rows, so all that was needed to fix the problem was initializing the length[nets] array to zero with a simple for loop

    int length[nets]
    for (i=0;i<=nets-1;i++)
    length[i]=0;
    Last edited by engg; 10-16-2011 at 03:44 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And you are still going outside your array's boundaries...

    What you have is working by the simple virtue of overlapping accesses and plain old fashioned dumb luck.

    Consider that nets = 5 (just for easy math) ... that means you can have length[0], lenght[1], length[2], length[3], length[4] ...
    Again, count them, that's 5 elements...
    The minute you write to length[5] you are outside your array bounds... writing on memory you don't own.

    You apprear to be making a very common error... confusing "the number of elements" with "the number of the element"... in C all arrays run from 0 to size-1 ... always.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    no guy, im not making an error- get over yourself.
    Im VERY aware that arrays start at 0, that is not and was not ever the issue. Adding a simple -1 (which is in the code) works just fine and certain applications make <= a better option.
    Don't act like you're a genious pointing out basic observations when you have no idea what the program is doing or why it has problems in the first place and don't get offended when your lame excuse for advice proves useless

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by engg View Post
    no guy, im not making an error- get over yourself.
    Im VERY aware that arrays start at 0, that is not and was not ever the issue. Adding a simple -1 (which is in the code) works just fine and certain applications make <= a better option.
    Don't act like you're a genious pointing out basic observations when you have no idea what the program is doing or why it has problems in the first place and don't get offended when your lame excuse for advice proves useless
    I keep forgetting that people who've been programming for a year or less are so much smarter than those of us with 20 or 30 years experience.

    In any case...
    Code:
    for(i=0;i<=nets-1;i++)
    reduces to
    Code:
    for(i=0; i < nets; i++)
    but it doesn't have to calculate nets-1 every time through the loop and it doesn't need to test for both less and equality every time through the loop. Probably won't make much difference on your chore but it does make quite the difference when working through a loop of 1,000,000 calculations...
    Last edited by CommonTater; 10-16-2011 at 08:54 PM.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    engg, I think you are still going outside array bounds.

    You are doing this:
    Code:
    for(j=0;j<=length[i]-1;j++) {
    /* other stuff */
       for(k=j+1;k<=length[i];k++) {
          /* do something with tmpmatrix[i][j] and tmpmatrix[i][k] */
       }
    }
    Why do you have a -1 in the j loop but not in the k loop?
    CommonTater likes this.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    engg, I think you are still going outside array bounds.

    You are doing this:
    Code:
    for(j=0;j<=length[i]-1;j++) {
    /* other stuff */
       for(k=j+1;k<=length[i];k++) {
          /* do something with tmpmatrix[i][j] and tmpmatrix[i][k] */
       }
    }
    Why do you have a -1 in the j loop but not in the k loop?
    Like I told him! Per usual in these situations where people engage in bad programming practices, when you find one there's usually more which is why I wasn't specific... I wanted him to check and fix ALL of his loops.
    Last edited by CommonTater; 10-16-2011 at 09:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with strange error
    By fabriciomind in forum C Programming
    Replies: 2
    Last Post: 06-03-2011, 07:28 AM
  2. strange error int
    By faluiretoread in forum C++ Programming
    Replies: 10
    Last Post: 11-15-2008, 05:51 PM
  3. strange error
    By peking1983 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 09:58 PM
  4. strange error!?
    By pode in forum Game Programming
    Replies: 3
    Last Post: 12-26-2002, 12:17 PM
  5. Strange error.
    By Will in forum C++ Programming
    Replies: 9
    Last Post: 05-01-2002, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21