为什么这不产生分段错误 [英] Why does this not produce a segmentation fault

查看:108
本文介绍了为什么这不产生分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在c ++中的for循环。我不明白为什么它不执行分割错误。

  int main()
{
int arr [5];
for(int x = 0; x <= 5; x ++)
{
arr [x] = x;
}
return 0;
}


解决方案

行为。未定义的行为意味着可能发生任何,包括:




  • 不会出现任何错误
  • 不一致的输出

  • 格式化硬盘驱动器




为了更正式一点,C ++ 11标准定义了未定义的行为:


行为本国际标准没有强制要求
[注意:当本国际标准省略任何
行为的明确定义,或者当程序使用错误的构造或错误的数据时。 允许未定义的行为
的范围包括完全忽略具有不可预测的结果的情况,在翻译期间行为,或以记录的环境特征的
程序执行
(有或没有发布
a诊断消息),终止翻译或执行(发送诊断消息)。
许多错误的程序结构不会产生未定义的行为;他们需要诊断。
-end note]


有关 x [5] 确实是未定义的行为,这是因为 x [5] 等价于 *(x + 5)见第8.3.4 / 6段)和第5.3.1 / 1段关于一元运算符 * 指定:



< >

unary *运算符执行间接:应用它的表达式应该是指向
对象类型的指针,或者指向函数类型的指针,结果是lvalue引用表达式所指向的对象或函数
。如果表达式的类型是指向T的指针,结果的类型是
T。[...]


但是因为 x + 5 不指向任何对象,并且上面的段落没有指定解引用这样的指针的结果,引用的句子适用:


[...]本国际标准没有明确的行为定义, ]


这意味着 x [5] 是未定义的行为。 p>

This is a for loop in c++. I dont understand why it does not give a segmentation fault on execution.

int main() 
{
   int arr[5];
   for (int x = 0; x <= 5; x++)
       {
           arr[x] = x;
       }
   return 0; 
}

解决方案

This is undefined behavior. Undefined behavior means that anything can happen, including:

  • segmentation fault
  • no error at all
  • inconsistent output
  • formatting of hard drive
  • ... (whatever)

To get a bit more formal, this is how the C++11 Standard defines undefined behavior:

behavior for which this International Standard imposes no requirements [ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]

Concerning the reason why doing x[5] is indeed undefined behavior, that's because x[5] is equivalent to *(x + 5) (see paragraph 8.3.4/6), and paragraph 5.3.1/1 about the unary operator * specifies:

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is "pointer to T," the type of the result is "T." [...]

But since x + 5 does not point to any object, and the above paragraph does not specify what the result of dereferencing such a pointer should be, the previously quoted sentence applies:

[...] Undefined behavior may be expected when this International Standard omits any explicit definition of behavior [...]

Which means that x[5] is undefined behavior.

这篇关于为什么这不产生分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆