W
wearyofallthiscrap
(I'm posting this via Google, I apologize if the From: line is
completely
broken.)
I'm trying to track down a bug in some code which handles memory
buffers.
I'm not accustomed to using C, and there's an idiom which is confusing
me.
The buffer uses head (writing) and tail (reading) offsets.
Calculating the
size is being done with lines like this:
end = (head + (buffer_size - tail)) % buffer_size;
size = end;
Now, I understand (I think) why the math is ordered the way it is, to
avoid any potential integer overflow when "head + buffer_size" would
have
been calculated. What I don't understand is why
size = head - tail;
would not have been sufficient? What's the point of "wrapping" the
numbers around the buffer, so to speak?
completely
broken.)
I'm trying to track down a bug in some code which handles memory
buffers.
I'm not accustomed to using C, and there's an idiom which is confusing
me.
The buffer uses head (writing) and tail (reading) offsets.
Calculating the
size is being done with lines like this:
end = (head + (buffer_size - tail)) % buffer_size;
size = end;
Now, I understand (I think) why the math is ordered the way it is, to
avoid any potential integer overflow when "head + buffer_size" would
have
been calculated. What I don't understand is why
size = head - tail;
would not have been sufficient? What's the point of "wrapping" the
numbers around the buffer, so to speak?