What is the simplest standard conform way to produce a Segfault in C?

后端 未结 10 1630
迷失自我
迷失自我 2020-11-29 21:59

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:

10条回答
  •  萌比男神i
    2020-11-29 22:26

    On some platforms, a standard-conforming C program can fail with a segmentation fault if it requests too many resources from the system. For instance, allocating a large object with malloc can appear to succeed, but later, when the object is accessed, it will crash.

    Note that such a program is not strictly conforming; programs which meet that definition have to stay within each of the minimum implementation limits.

    A standard-conforming C program cannot produce a segmentation fault otherwise, because the only other ways are via undefined behavior.

    The SIGSEGV signal can be raised explicitly, but there is no SIGSEGV symbol in the standard C library.

    (In this answer, "standard-conforming" means: "Uses only the features described in some version of the ISO C standard, avoiding unspecified, implementation-defined or undefined behavior, but not necessarily confined to the minimum implementation limits.")

提交回复
热议问题