Originally Posted by
john.c
Are you saying that sizes of 1 to 191 all "work"?
Are you saying that sizes of 192 to 255 all "don't work"?
Try to answer Salem's questions:
1. What machine are you running this on? Are you running it at all?
2. What compiler/etc. do you use to build the executable?
3. What exactly happens? "It doesn't work" says very little. How may chars are transmitted before the problem? Does it halt (crash)? Does it start overwriting data, outputting garbage, what?
The tricky points in the code have to do with unsigned char having a range of 0 to 255. So in and out will wrap around from 255 to 0 when they hit the end of that range. At some points, in will be less than out, so in - out will be negative. That would seem to be a problem with any buffer size.
Also, the "modded index" will jump when the value wraps if the buffer size doesn't divide 256 evenly. E.g., buffer size = 191: 255 % 191 = 64, 0 % 191 = 0. So the buffer index jumps from 64 to 0.
Like Salem said, the answer seems to be to keep in and out within the buffer bounds at all times. E.g., instead of ++in, use in = ( in + 1 ) % sizeof(buffer)