“电势除以零”是什么意思? Visual C 中的意思是++ C4723 警告描述?
当我在 Visual C++ 10 中编译以下代码时,
int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
int len = strlen( "" );
if( len / 0 ) {
rand();
}
}
编译器发出 C4723 警告 潜在除以零。
这里的潜力是什么意思?我的 C++ 代码明确表示“将 len
除以零”,除法的潜力如何?
When I compile the following code in Visual C++ 10
int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
int len = strlen( "" );
if( len / 0 ) {
rand();
}
}
the compiler emits C4723 warning potential divide by zero.
What does potential mean here? My C++ code clearly says "divide len
by zero", how is the divide potential?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MSDN 文章清楚地表明,编译器在编译时已将操作数评估为零。所以这里的潜在意味着编译器只不确定一件事——这段代码是否会被执行。
MSDN article is clear that compiler evaluated operand to be zero already at compile time. So
potential
here means that compiler is unsure about one thing only - whether this code is going to be ever executed or not.意思是一样的,只是出于礼貌。
你想让它说“你正在除以零吗,白痴!” ? (-`
It means the same, it's just been polite.
Would you like it to say "You are dividing by zero, idiot !" ? (-`
编译器不会假设执行路径会到达被 0 除的位置。
这是一个合理的假设,因为 _tmain 的执行是在编译之后决定的。
The compiler doesn't assume the execution path will reach the division by 0.
It's a reasonable assumption, because execution of _tmain is decided after the compilation.
假设您是编译器开发人员,并且您创建了一个要在编译期间运行的静态分析工具,以帮助捕获错误。
例如,这里是一个基于范围的引擎,它将确定
/
右侧运算符可能采用的可能值。现在有两种情况:
显然,区分这两种情况需要更多的努力。
因此,您可能会正确地假设最常见的错误原因是可能性,而不是确定性(正常人的 nodoby 会除以 0,对吗?)为自己节省一些工作。
这是最佳的吗?对于开发人员来说也许,对于用户来说则不然。
可以用吗?当然可以。
Say you are a compiler developer and you create a static analysis tool to be run during the compilation to help catch errors.
For example, here, a range-based engine that will determine the possible values that the right-hand operator of
/
could take.Now there are two cases:
Obviously, separating the two cases requires more effort.
And thus you might rightly assume that the most common cause of errors will be a possibility and not a certainty (nodoby in its right mind would divide by 0, right ?) and save yourself some work.
Is it optimal ? For the developer perhaps, for the user not really.
Is it usable ? Definitely.