The cause of the segmentation fault is unknown.

Asked 2 years ago, Updated 2 years ago, 29 views

I compiled the following programs and ran them, and they said Segmentation fault.

Is there something wrong?

#include<stdio.h>

#define N10
#define NUM_SCORE50

int main (void) 
{

    inti,j,n;
    int score [NUM_SCORE] = {
        1,  4,  9,  9,  8, 10, 10,  9,  5, 10, 
        2,  9,  6,  4,  0,  7,  3,  5,  6,  6, 
        7,  4,  2,  9,  2,  5,  5,  3,  1,  9, 
        5,  7,  3,  2,  7,  9,  1,  7,  6,  6, 
        5,  8,  2,  5,  3, 10,  6,  2,  2,  5, 
    };

    int histogram[N+1]; /* Array containing the number of students with score i.histogram[i]*/

    for (i=0; i<=N;i++)
    {   
        histogram[i] = 0;
    }
    
    for (j=0;j<=NUM_SCORE;j++)
    {
        histogram [score[j]]++;
    }

    /* View Results*/
    printf("The distribution of points is as follows.\n";
    for (i=0; i<=N;i++) 
    {
        printf("%2d point: %d\n", i, histogram[i]);
    }
    n = 0;
    for (i=7;i<=N;i++)
    {
        n = n + histogram [i];
    }
    printf("%d people get more than 7 points",n);
    
    return 0;

}

c

2022-09-29 21:31

2 Answers

It's a compound condition.
The first reason is that the continuity condition j<=NUM_SCORE in this line contains =, which exceeds the size of the array score.

 for (j=0;j<=NUM_SCORE;j++)

This is the score declaration

 int score [NUM_SCORE] = {

If so, valid index values range from 0 to (NUM_SCORE-1).
Based on the data outside the score range, you may have accessed further outside the array histogram (this is not a maximum value of +1, but a larger value).
That's probably why it became a segmentation fault.

histogram [score[j]]++;

forRemove = from the loop continuity condition and set it to NUM_SCORE to work fine.

 for (j=0;j<NUM_SCORE;j++)

Loops using another N have instistogram [N+1]; reserved in N+1 areas, so this is

 for (i=0;i<=N;i++)

and so on

 for (i=7;i<=N;i++)

There is no problem with looping with .
(However, there is no problem with the range of index values in the array, but whether or not there is no problem with the actual action to be taken is not a problem.This time it looks fine, but you should be careful about the meaning of the #define macro numbers.)

Another misleading reason is the mix of ways to declare the number of elements in an array in a short program.


2022-09-29 21:31

For your reference, if your C compiler is gcc, you have the option to display warning for out-of-range access ( out of bounds access).

gcc(1)

-Warray-bounds
-Warray-bound=n
 This option is only active when-free-vrp is active (default for-O2 and above).It wars about subscripts to arrays that are always out of bounds.This warning is enabled by-Wall.

-Warray-bound=1
  This is the warning level of -Warray-bound and is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Warray-bound=2
  This warning level also warnings about out of bounds access for arrays at the end of a structure and for arrays accessed through pointers. This warning level may give a warning number of false positives and is deactivated by default.

$gcc --version
gcc (Ubuntu 9.3.0-17 ubuntu 1 to 20.04) 9.3.0

$ gcc-O2-Warray-bounds-ohistogram histogram.c
histogram.c:In function 'main':
histogram.c:27:21:warning:iteration 50 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 | histogram [score[j]]++;
      |                ~~~~~^~~~~
histogram.c:25:3:note:within this loop
   25 | for (j=0; j<=NUM_SCORE;j++)
      |   ^~~

Other results can be obtained using cppcheck-A tool for static C/C++ code analysis.

$cppcheck --enable=all histogram.c 
Checking histogram.c...
histogram.c:27:21:error:Array'score [50]'accessed at index 50, which is out of bounds. [arrayIndexOutOfBounds]
    histogram [score[j]]++;
                    ^


2022-09-29 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.