Thread: Logic error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Logic error


    Hi All,

    If someone can point out where I'm going wrong with this it would be really appreciated!

    As far as I can work out, I've got a logic error somewhere as the results are not as expected. i.e. the histogram produced doesnt match to what i type.

    C Code:
    int main()
    {
       int state, c, lc;
       int wordl[20];
     
       for(int j = 0; j<19; j++)
          wordl[j]=0;
     
       state = OUT;
       lc = 0;
     
       while((c = getchar()) != EOF){
          if (c== ' '|| c=='\n' || c=='\t' || lc >= MAXLEN){
     
             state=OUT;
     
             if (lc>=MAXLEN){
                ++wordl[MAXLEN-1];
             }else{
                ++wordl[lc];
             }
     
          } else if (state == OUT){
             state = IN;
             lc = 0;
     
          } else {
             ++lc;
     
          }
       }
     
       for(int j = 19; j>=0; j--){
          printf("%d: \t", j);
          c=wordl[j];
          while(c>0){
             printf("*");
             c--;
          }
          printf("\n");
       }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    What results are you getting?

    What results do you expect?

    Jim
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    You should also provide us with the input that you are using.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    I'm entering a series of words of varying length, the words themselves are arbitrary. My issue is that when I use words of over 19 chars in length, the count goes wrong.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Then don't use words over 19 characters in length! Or make wordl larger, large enough to contain that longest word that you will input plus one more character for the null-terminator.

    You are overflowing your buffer which results in you clobbering the variables next to wordl.
    Code:
       int state, c, lc;
       int wordl[20];
    That means that you are overwriting them with the extra characters that overflow out of wordl. Which means that first lc, then c, and then state get frakked up.

    Corrective action: Either make wordl large enough or else enter no word longer than 19 letters long.

    BTW, buffer overflow is a common hacker exploit. Never allow buffer overflow to happen!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Thanks for the response, that actually makes a lot more sense to me.

IMN logo majestic logo threadwatch logo seochat tools logo