c++ assertion

21 篇文章 0 订阅

from usaco: usacoprobfix.htm

  • Put sanity checks around all array indexing (Pascal does this by default). The assert() (from assert.h) routine is a nice tool for this. assert() fails if the parameter is 0 (false). If this happens, assert will stop the program and print out an error message saying which assert on which line failed. The really nice thing about assert is that you can #define NDEBUG for including assert.h and the assert operations go away, so that you can put a lot of debugging in and not run it when you want the code to be fast. Consider:
         int i, j, quotient;
         i = 8;
         j = 0;
         assert (j != 0);
         quotient = i/j;
    
    or
         int i, j, chessboard[8][8];
         assert (0 <= i);
         assert (0 <= j);
         assert (i < 8);
         assert (j < 8);
    
    Why four statements? Because the compound statement doesn't give you enough data when failure occurs. 

google c++ style

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值