在 C 中产生 Segfault 的最简单的标准符合方法是什么? [英] What is the simplest standard conform way to produce a Segfault in C?

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

问题描述

我认为这个问题说明了一切.涵盖从 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
", s[0] );
  return 0;
}

由于一些投票要求澄清:我希望有一个程序具有通常的编程错误(我能想到的最简单的是段错误),即保证(按标准)中止.这与最小的 segfault 问题有点不同,后者不关心这个保险.

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.

推荐答案

分段错误是实现定义的行为.该标准没有定义实现应该如何处理未定义的行为,实际上实现可以优化未定义的行为并且仍然是合规的.需要明确的是,实现定义的行为是不是指定的行为标准,但实施应记录.未定义的行为是不可移植或错误的代码,其行为不可预测,因此不能依赖.

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 段中说:

注意可能的未定义行为范围从完全忽略具有不可预测结果的情况,到在翻译或程序执行期间以环境特征的记录方式表现(无论是否发出诊断消息),到终止翻译或执行(发出诊断消息).

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 系统上导致分段错误,那么 raise(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 信号处理<signal.h>说:

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

实现不需要生成任何这些信号,除非是显式调用 raise 函数的结果.附加的信号和指向不可声明函数的指针,宏定义分别以字母 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 中产生 Segfault 的最简单的标准符合方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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