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

后端 未结 10 1647
迷失自我
迷失自我 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条回答
  •  囚心锁ツ
    2020-11-29 22:29

    A correct program doesn't produce a segfault. And you cannot describe deterministic behaviour of an incorrect program.

    A "segmentation fault" is a thing that an x86 CPU does. You get it by attempting to reference memory in an incorrect way. It can also refer to a situation where memory access causes a page fault (i.e. trying to access memory that's not loaded into the page tables) and the OS decides that you had no right to request that memory. To trigger those conditions, you need to program directly for your OS and your hardware. It is nothing that is specified by the C language.

提交回复
热议问题