Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I believe they only standardized the two's-complement representation (so casts to unsigned have a more specific behavior, for example) but they did not make overflow defined.


Yeah, signed integer overflow is as UB as ever. I've heard the primary reason for it is to avoid the possibility of wraparound on 'for (int i = 0; i < length; i++)' loops where the 'length' is bigger than an int. (Of course, the more straightforward option would be to use proper types like size_t for all your indices, but it's a classic tradition to use nothing but char and int, and people judge compilers based on existing code.)


ptrdiff_t is also useful in this case if signed semantics are desired.


> I've heard the primary reason for it is to avoid the possibility of wraparound on

Making it UB doesn’t fix that in any way that I can think of.


What it means is that since i as the variable is monotonically increasing, an array indexing operation that is in the loop body can be replaced with an incrementing pointer instead, which eliminates quite a lot of code. An example here: https://pvs-studio.com/en/blog/posts/cpp/0374/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: