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


i think question says all. example covering standards c89 c11 helpful. though of one, guess undefined behaviour:

#include <stdio.h>  int main( int argc, char* argv[] ) {   const char *s = null;   printf( "%c\n", s[0] );   return 0; } 

edit:

as votes requested clarification: wanted have program usual programming error (the simplest think of segfault), guaranteed (by standard) abort. bit different minimal segfault question, don't care insurance.

a segmentation fault implementation defined behavior. standard not define how implementation should deal undefined behavior , in fact implementation optimize out undefined behavior , still compliant. clear, implementation defined behavior behavior not specified standard implementation should document. undefined behavior code non-portable or erroneous , behavior unpredictable , therefore can not relied on.

if @ c99 draft standard §3.4.3 undefined behavior comes under terms, definitions , symbols section in paragraph 1 says (emphasis mine going forward):

behavior, upon use of nonportable or erroneous program construct or of erroneous data, this international standard imposes no requirements

and in paragraph 2 says:

note possible undefined behavior ranges ignoring situation unpredictable results, behaving during translation or program execution in documented manner characteristic of environment (with or without issuance of diagnostic message), terminating translation or execution (with issuance of diagnostic message).

if, on other hand, want method defined in standard cause segmentation fault on unix-like systems raise(sigsegv) should accomplish goal. although, strictly speaking, sigsegv defined follows:

sigsegv invalid access storage

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

an implementation need not generate of these signals, except result of explicit calls raise function. additional signals , pointers undeclarable functions, macro definitions beginning, respectively, letters sig , uppercase letter or sig_ , uppercase letter,219) may specified implementation. the complete set of signals, semantics, , default handling implementation-defined; signal numbers shall positive.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -