off-by-one error

今天看书有一个off-by-one error,感觉很陌生,google了一下,wiki上有一些资料。

Consider an array of items, and items m through n (inclusive) are to be processed. How many items are there? An intuitive answer may be n−m, but that is off by one, exhibiting afencepost error; the correct answer is n−m+1.

For this reason, ranges in computing are often represented by half-open intervals; the range from m to n (inclusive) is represented by the range from m (inclusive) to n+1 (exclusive) to avoid fencepost errors. For example, a loop that iterates five times can be written as a half-open interval from 0 to 5:

for (i = 0; i < 5; i++) {
    
}

The loop body is executed first of all with i equal to 0; i then becomes 1, 2, 3, and finally 4 on successive iterations. At that point, i becomes 5, so i < 5 is false and the loop ends. However, if the comparison used were <= (less than or equal to), the loop would be carried out six times: i takes the values 0, 1, 2, 3, 4, and 5. Likewise, if i were initialized to 1 rather than 0, there would only be four iterations: i takes the values 1, 2, 3, and 4. Both of these alternatives can cause off-by-one errors.

Another such error can occur if a do-while loop is used in place of a while loop (or vice versa.) A do-while loop is guaranteed to run at least once.

Array-related confusion may also result from differences in programming languages. Numbering from 0 is most common, but some languages start array numbering with 1. Pascal has arrays with user-defined indices. This makes it possible to model the array indices after the problem domain.


一大堆英文,哦……no。

简单说的,off-by-one error就是在说数组越界(我的理解),它包含两种情况。就像例子中的for循环,它定义了一个大小为5的数组,如国我们误将判断条件 i < 5 写成了 i <= 5,那么就会触发一个错误,这就是 off-by-one error。还有一种情况是我们将 i 初始化为 1,同样是一个 off-by-one error。

例子中就说了这两种情况,希望大家能够理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<html> <head> <title>登录界面</title> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style type="text/css"> .divForm{ position: absolute; width: 300px; height: 280px; top: 30%; left: 40%; margin-top: 1px; margin-left: 5px; } .title{ width:100%; height:50px; font-size:38px; line-height:50px; margin-bottom:20px; font-family: 华文宋体; color: cornflowerblue; } </style> </head> <body> <div class="divForm"> <form method="post" action="${pageContext.request.contextPath}/login/judgeLogin"> <div class="title" >&nbsp;&nbsp;&nbsp;欢 迎 登 录</div> <div class="form-group has-success has-feedback"> <label class="control-label">&nbsp;&nbsp;用户名: &nbsp;&nbsp;<input type="text" class="form-control" name="userName" id="userName" autocomplete="off" placeholder="请输入用户名"> </label> </div> <div class="form-group has-success has-feedback"> <label class="control-label">&nbsp;&nbsp;密码: <input type="password" class="form-control" name="userPwd" id="userPwd" autocomplete="off" placeholder="请输入密码"> </label> </div> <div > <a onclick="jumpRegister()">&nbsp;&nbsp;&nbsp;&nbsp;注册账号</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">登录</button> </div> </form> </div> <script> function jumpRegister() { window.location = "${pageContext.request.contextPath}/register/index"; } </script> </body> </html>在此基础上添加一个图片背景
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值