The cause of the segmentation fault is unknown.

Asked 2 years ago, Updated 2 years ago, 28 views

After compiling and running the program below, you can enter the number at the bottom to see Segmentation fault.
Is there something wrong?
If you don't mind, please let me know.

#include<stdio.h>

#define N10
#define NUM_SCORE50

int main (void) 
{

    inti,j,n,x;
    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, 
    };

    inthistogram [N+1]; 

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


    printf("The distribution of points is as follows.\n";
    for (i=0; i<=N;i++) 
    {
        printf("%2d point: %d\n", i, histogram[i]);
    }
    
    printf("Please enter an integer >>>>");
    scanf("%d", x);
    n = 0;
    for (i=x; i<=N+1;i++)
    {
        n = n + histogram [i];
    }
    printf("%d people over %d",x,n);
    return 0;

}

c

2022-09-29 21:34

2 Answers

@Jogenara also pointed out that the reason for the segmentation fault is that the added scanf("%d", x); specifies the pointer to the variable that stores the entered value, but the variable itself (and not initialized).

The result of converting the entered numeric string to int is probably a segmentation fault because it tries to store the value of the uninitialized variable as a pointer in the area to which it is indicated.
Well, even if it was initialized (mostly zero), segmentation fault occurs because it is an invalid value for a pointer (address).

cppcheck introduced @metropolis in the previous answer shows the following:
(I'm checking the Windows GUI screen, so the details may be different.)

CWE:686
%d in format string(no.1) requires 'int*' but the argument type is 'signed int'.

@Jogenara As pointed out, x should be preceded by & to make it scanf("%d", & x);.

Also, in the comment to the previous answer, printf and scanf were added only, but other parts (loop continuity conditions) have been changed, which is also a problem.
Originally:

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

New:

 for (i=x;i<=N+1;i++)

i<=N is i<=N+1.
This will cause problems (segmentation fault, or difference in total number of people) below the loop.

n=n+histogram[i];

Similar to above, cppcheck displays the following "error" in the line n=n+histogram[i]; above:

CWE:788
Array'histogram[11]'accessed at index 11, which is out of bounds.

Please revert to the original i<=N.


2022-09-29 21:34

 for (i=x;i<=N+1;i++)
{
    n = n + histogram [i];
} 

It's here.
I'm referring to the out-of-array.
Other places seem to be paying attention, so
It's a bug that I noticed when I was careful.

If you look closely, the scanf was wrong.
scanf("%d", & x); is correct.


2022-09-29 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.