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

math picture math · Sep 24, 2013 · Viewed 20.7k times · Source

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;
}

EDIT:

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.

Answer

msam picture msam · Sep 24, 2013

raise() can be used to raise a segfault:

raise(SIGSEGV);