Archived

This topic is now archived and is closed to further replies.

why would a loop go past its max?

Recommended Posts

chbfiv    139
printf("Srtlen=%d\n",strlen(string)+1);

for(int i=0;i <=strlen(string)+1;i++) {
printf("char_%d=%c\n",i,string[i]);
}

why would i ever be larger then the strlen+1? output of this while looping through strings in a file(fgets), only one line does this. Strlen=213 and it prints out char''s up to 220=( why the heck did it even get to 214?

Share on other sites
ToohrVyk    1596
First, try like this:

long stringsize = strlen(string)+1;printf("Len = %d", stringsize );for( int i = 0; i <= stringsize; ++i ) {      printf("\nchar %d = %c", i, string[i] );}

If this still doesn''t work, well...

ToohrVyk

Share on other sites
Nerusai    103
(dang, someone was a little bit faster, but took less effort too )

Hint: Make it more readable next time...

printf ( "Srtlen=%d\n", strlen ( string ) + 1 );for (int i = 0; i <= strlen ( string ) + 1; i++ ){    printf ( "char_%d=%c\n", i, string [i] );}

For one, calling strlen every cycle of the loop is plain stupid - if the string is constant, make it like this:

For two, why would you want to use strlen + 1???

   size_t stringlength;stringlength = strlen ( string );printf ( "Srtlen=%d\n", stringlength );for ( int i = 0; i < stringlength; i++ ){    printf ( "char_%d=%c\n", i, string [i] );}

Correct me if I'm wrong, but my piece of code will print out the entire string on the screen in the way you planned it.

Edit by Siaon: Changed the code tags to source tags.

Another edit: Damned forum changes what I type in?? :/

[edited by - Siaon on August 11, 2003 2:07:17 PM]

[edited by - Siaon on August 11, 2003 2:08:30 PM]

[edited by - Siaon on August 11, 2003 2:09:22 PM]

Share on other sites
foofightr    130
quote:
Original post by chbfiv

why the heck did it even get to 214?

Because you''re accessing two bytes past the end of the array. One by using <= instead of < and another with the +1 you artificially stuck in there.

As for why it gets to 220, it shouldn''t, given the code you showed us.

Share on other sites
Way Walker    745
quote:
Original post by foofightr
Because you''re accessing two bytes past the end of the array. One by using <= instead of < and another with the +1 you artificially stuck in there.

strlen(myString) returns the length of myString excluding the terminating nul. strlen(myString)+1 returns the length of myString including the terminating nul.

quote:

As for why it gets to 220, it shouldn''t, given the code you showed us.

For a slightly unreasonable answer, and assuming you''ve allocated exactly 213 char''s, then you''ve got some undefined behavior by dereferencing the 214th char. Technically, this means anything can happen, but I wouldn''t guess that''s the problem.

Another, similar, answer is that perhaps sizeof(int) != sizeof(size_t) on your platform. strlen() returns size_t, printf is variadic, variadic functions have typesafety issues. What does this mean? That you should never lie to a variadic function about what you''re passing and always cast to the type you told the function to expect (i.e. don''t pass a int* when it''s expecting void*, etc.). Why does this matter? printf only read sizeof(int) bytes out of sizeof(size_t) bytes that you passed, which could mean that you have something other than 213 char''s in your string.

Other than that, I''m not sure.