Infinite loop when using size_t in a count down for loop

后端 未结 7 1153
走了就别回头了
走了就别回头了 2021-01-13 07:53

So I\'m using size_t instead of int in any indexing for loop to prevent negative indices. But when counting down, this leads to an overflow:

<
7条回答
  •  不知归路
    2021-01-13 08:26

    for (size_t i = 10; i <= 10; --i) // do something
    

    When overflow do happens, it will round to the largest integer and thus the condition will fail.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题