什么是最简单的标准一致的方式在C中产生一个段错误? [英] What is the simplest standard conform way to produce a Segfault in C?

查看:88
本文介绍了什么是最简单的标准一致的方式在C中产生一个段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这个问题说明了一切。涵盖了大部分的标准从C89到C11的一个例子将是有益的。不过,我觉得这一个,但我想这只是未定义的行为:

I think the question says it all. An example covering most standards from C89 to C11 would be helpful. I though of this one, but I guess it is just undefined behaviour:

#include <stdio.h>

int main( int argc, char* argv[] )
{
  const char *s = NULL;
  printf( "%c\n", s[0] );
  return 0;
}

编辑:

由于一些选票要求澄清:我想有一个与通常的编程错误(我能想到的最简单的是一个段错误)的程序,也就是的保证的(按标准)中止。这是最小的内存设计缺陷问题,不关心这个保险的有点不同。

As some votes requested clarification: I wanted to have a program with an usual programming error (the simplest I could think of was an segfault), that is guaranteed (by standard) to abort. This is a bit different to the minimal segfault question, which don't care about this insurance.

推荐答案

分割故障是的实现定义的行为的。该标准没有定义的实施应处理的未定义行为事实上的实施可以优化出的未定义行为的,仍然是标准。需要明确的是,实现定义的行为的是它是不是指定行为标准但执行应记录。的未定义行为的为code,它是不可移植的或错误的,其行为是未predictable,因此不能依靠。

A segmentation fault is an implementation defined behavior. The standard does not define how the implementation should deal with undefined behavior and in fact the implementation could optimize out undefined behavior and still be compliant. To be clear, implementation defined behavior is behavior which is not specified by the standard but the implementation should document. Undefined behavior is code that is non-portable or erroneous and whose behavior is unpredictable and therefore can not be relied on.

如果我们看一下 C99标准草案&教派; 3.4.3的未定义行为的随附下的术语,定义和符号的段部分的 1 的它说(强调矿井前进的):

If we look at the C99 draft standard §3.4.3 undefined behavior which comes under the Terms, definitions and symbols section in paragraph 1 it says (emphasis mine going forward):

行为,在使用不可移植的或错误的程序结构的或者错​​误的数据,其中这个国际标准并没有规定要求

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

和段落的 2 的说道:

请注意可能的不确定的行为从与联合国predictable结果完全无视的情况下,在环境的记录方式的特点翻译或程序执行过程中的行为(有或没有发行的诊断消息)的范围,以终止翻译或执行(并出具诊断消息)。

NOTE Possible 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).

如果,在另一方面,你只是想在标准中定义的方法,将导致分割故障在大多数的的类Unix 的系统则加薪(SIGSEGV)要实现这一目标。虽然严格来说, SIGSEGV 定义如下:

If, on the other hand, you simply want a method defined in the standard that will cause a segmentation fault on most Unix-like systems then raise(SIGSEGV) should accomplish that goal. Although, strictly speaking, SIGSEGV is defined as follows:

SIGSEGV的非法访问存储

SIGSEGV an invalid access to storage

和与教派; 7.14的信号处理&LT;&signal.h中GT; 的说道:

and §7.14 Signal handling <signal.h> says:

的实现不需要生成任何这些信号,除非显式调用raise函数的结果。额外的信号和指针undeclarable功能,与宏定义开始,分别用字母SIG和大写字母或SIG_和大写字母,219),也可以通过执行规定。 的一套完整的信号,它们的语义,它们的默认处理是实现定义;所有信号数字应是积极的。

An implementation need not generate any of these signals, except as a result of explicit calls to the raise function. Additional signals and pointers to undeclarable functions, with macro definitions beginning, respectively, with the letters SIG and an uppercase letter or with SIG_ and an uppercase letter,219) may also be specified by the implementation. The complete set of signals, their semantics, and their default handling is implementation-defined; all signal numbers shall be positive.

这篇关于什么是最简单的标准一致的方式在C中产生一个段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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