Re: Why "segmentation fault"?
From: Floyd L. Davidson (floyd_at_barrow.com)
Date: 03/01/05
- Next message: Morfean: "Re: Kernel panic due to NF_IP_LOCAL_OUT handler calling itself again"
- Previous message: Kasper Dupont: "Re: Why "segmentation fault"?"
- In reply to: Anders Christensen: "Why "segmentation fault"?"
- Next in thread: Anders Christensen: "Re: Why "segmentation fault"?"
- Reply: Anders Christensen: "Re: Why "segmentation fault"?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Morfean: "Re: Kernel panic due to NF_IP_LOCAL_OUT handler calling itself again"
- Previous message: Kasper Dupont: "Re: Why "segmentation fault"?"
- In reply to: Anders Christensen: "Why "segmentation fault"?"
- Next in thread: Anders Christensen: "Re: Why "segmentation fault"?"
- Reply: Anders Christensen: "Re: Why "segmentation fault"?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|