Strange things happening with counter

This is a discussion on Strange things happening with counter within the C Programming forums, part of the General Programming Boards category; Ok, this is guaranteed to be something silly which I'm missing, but I cannot see it at the moment. Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Strange things happening with counter

    Ok, this is guaranteed to be something silly which I'm missing, but I cannot see it at the moment.

    Code:
    char line[] = "00:name:type:ssid:secretsecretsecretsecretsecretsecret\n";
    csflsplit2(line, sizeof(line)/sizeof(char), ':');
    
    void csflsplit2(char csfline2[], int len, char sep) {
         char connarr[5][64];
         int i, y;
         int x = 0;
         
         for (i=0 ; i<len ; i++) {
              if (csfline2[i] == sep) {
                   y = i-1;
                   printf("%s\n", strndup(csfline2+x, (y+1)-x));
                   strcpy(connarr[i], strndup(csfline2+x, (y+1)-x));
                   x =+(y+2);
              }
              printf("i: %i\n", i);
         }
         printf("%s", strndup(csfline2+x, (y+1)-x));
         //strcpy(connarr[i+1], strndup(csfline2+x, (y+1)-x));
    }
    If I run this WITHOUT the red line, which copies the "substrings" into the connarr[][] array as it goes along, the output is fine. The substrings are written, and the i counter runs up to the array length and the for loop quits.

    Howver, if I INCLUDE the red line, copying the "substrings" into the connarr[][] array, the whole thing goes in a never-ending loop. After the i counter reaches 56, it's reset to zero and it all starts over again.

    Output WIHTOUT the strcpy line:
    i: 0
    i: 1
    00
    i: 2
    ...
    i: 6
    name
    i: 7
    ...
    i: 11
    type
    i: 12
    ...
    i: 16
    ssid
    i: 17
    ...
    i: 55
    secretsecretsecretsecretsecretsecret

    Output INCLUDING the strcpy line:
    i: 0
    i: 1
    00
    i: 2
    ...
    i: 6
    name
    i: 7
    ...
    i: 11
    type
    i: 12
    ...
    i: 16
    ssid
    i: 17
    ...
    i: 55
    secretsecretsecretsecretsecretsecret
    i: 0
    i: 1
    00
    i: 2
    ...
    etc.

    I know - there's obviously some basic logic which I'm missing here, but could someone a bit more "awake" than me point it out? I'd be really grateful :-)

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How about using strtok() ? and simplify your code?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ah, that's of course a good option. However, I'm really curious as to what's wrong in the logic here, so it'd still be fun to find out.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You pass sizeof(line)/sizeof(char) to the function which is 56. Then you attempt to assign connarr[i] but it has only been allocated for 5 entries. So when you start writing stuff past allocated memory you are munching your other loop variables.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Argh, dang. Of course. I'm trying to use the i counter for the 1-5 counting of the conarr[5][64] array, which of course doesn't work since i increases far beyond 5. Thanks :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. s(n)printf weird things happening...
    By KneeLess in forum Windows Programming
    Replies: 5
    Last Post: 10-15-2005, 05:55 PM
  2. Matrix Multiplication - Weird things happening
    By spoon_ in forum C Programming
    Replies: 4
    Last Post: 09-02-2005, 07:53 PM
  3. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 05:31 AM
  4. Funny things happening to values outside of function
    By drb2k2 in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2003, 02:39 PM
  5. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM

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