Re: Why "segmentation fault"?

From: Floyd L. Davidson (floyd_at_barrow.com)
Date: 03/01/05


Date: Tue, 01 Mar 2005 04:23:20 -0900

Anders Christensen <andersc1@hotmail.com> wrote:
>Hi,
>I have a strange segmentation fault in my C-program. It's is not appearing
>everytime the program is executed.
>
>Another reason for the fault to occur is this defective code:
> for (j=0;j<4;j++)
> sc[j].total_denied=sc[j].total_accepted=0; for (i=0;i<26;i++) {
> sc[j].invalid_format[i]=sc[j].invalid_date[i]=sc[j].invalid_time[i]=
> sc[j].tur[i]=sc[j].spa[i]=sc[j].accepted[i]=0;
> }

That is impossible to even read! I had to reformat it to even see
what was different from the "correct" code below.

  for (j = 0; j < 4; j++) {
    sc[j].total_denied = 0;
    sc[j].total_accepted = 0;
  }

  for (i = 0; i < 26; i++) {
    sc[j].invalid_format[i] = 0;
    sc[j].invalid_date[i] = 0;
    sc[j].invalid_time[i] = 0;
    sc[j].tur[i] = 0;
    sc[j].spa[i] = 0;
    sc[j].accepted[i] = 0;
  }

Notice that the second loop writes to an out of bounds array index,
where j = 4 (the 5th element in a 4 element array), which is what
causes your seg fault.

>Which should have been this instead (this does NOT generate the fault):
>
> for (j=0;j<4;j++) {
> sc[j].total_denied=sc[j].total_accepted=0; for (i=0;i<26;i++) {
> sc[j].invalid_format[i]=sc[j].invalid_date[i]=sc[j].invalid_time[i]=
> sc[j].tur[i]=sc[j].spa[i]=sc[j].accepted[i]=0;
> }
> }

Note that if you had formatted your code to be readable, the
difference if *obvious*. Here is the second fragment, which is
very clearly different than the reformatted fragment above, even
at a glance. Yet your two fragments look almost identical.

  for (j = 0; j < 4; j++) {
    sc[j].total_denied = 0;
    sc[j].total_accepted = 0;

    for (i = 0; i < 26; i++) {
      sc[j].invalid_format[i] = 0;
      sc[j].invalid_date[i] = 0;
      sc[j].invalid_time[i] = 0;
      sc[j].tur[i] = 0;
      sc[j].spa[i] = 0;
      sc[j].accepted[i] = 0;
    }
  }

>There are no errors when compiling the project with
> "gcc -Wall <filenames.c> ..."

You might consider also using -O and -W, which will cause other
warnings to be enabled. And then perhaps adding a few others...
Here are the options that I usually enable with gcc 3,

    -ansi -pedantic -Wall -W -O2 -g
    -Wcast-align -Wcast-qual
    -Wmissing-prototypes -Wshadow
    -Wnested-externs -Wstrict-prototypes
    -Waggregate-return

Note that none of these warnings will catch the error causing
the seg fault above, but a more readable coding style would have
made it easily spotted.

-- 
Floyd L. Davidson           <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@barrow.com


Relevant Pages

  • Re: Paceline Riding
    ... I was on a road bike and he was on a full-suspension mountain bike ... All depends on circumstance, but *still* his fault. ... predictably and give the necessary warnings and signals, ... Don't rip their legs off in thanks. ...
    (rec.bicycles.tech)
  • Re: C/C++
    ... If your C code takes no liberties, compile it as C++. ... errors you see are your fault. ... C permits many abuses that are not necessary ...
    (comp.lang.cpp)
  • Re: [patch 01/10] compiler: define __attribute_unused__
    ... my fault - I confused used and unused. ... Unused static non-inline functions are the only functions resulting in ... If we don't want gcc to emit warnings for such, ... There had been need of rain for many days. ...
    (Linux-Kernel)
  • Re: Serious problems after "apt-get dist-upgrade"
    ... That was likely my fault as I found the the ... > and paste all the warnings and do it by hand. ... Have you tried "aptitude"? ... To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org ...
    (Debian-User)