2 ms·
My C is rusty, but won't this act... oddly... on integer overflow? size() { return write - read; } 0 - UINT_MAX -1 = ? [EDIT] Changed constant to ref
by falcolas 10y ago
My C is rusty, but won't this act... oddly... on integer overflow?
size() { return write - read; }
0 - UINT_MAX -1 = ?
[EDIT] Changed constant to reflect use of unsigned integers, which I forgot to specify initially.
- crististm 10y agoActually, this method counts on it. What I find interesting are the trade-offs: machine vs explicit integer wrap-around and buffers with maximum ~size(int)/2 vs ~size(int).
- falcolas 10y agoGot it. Modular arithmetic was the term I was looking for to resolve this. (0 - (2^32 - 1)) % 2^32 = 1
- mfukar 10y agoIn all examples, `read` and `write` are unsigned, and since they both are the same type, no integer conversions are performed, ergo no overflow. PS. No wrap-around either, for different reasons.
- falcolas 10y ago> No wrap-around either, for different reasons. You'll have to explain that to me, since I can't assign `x = 2^32` without wraparound when x is an unsigned 32 bit integer.