Question

I'm writing code which takes input from the command line (a redirected text file) and sends the data to an array in c. However whenever I run it I get a segmentation fault (core dumped) error. Here is my code:

  int main(int argc, const char * argv[])
{
    char *experiments[20];
    int data[10][20];

int b=0;
char *name = NULL;
name=malloc(100);
sendToArray(data, experiments);
while(b==0){
    int input=0;
    printf("\n");
    printf("Data set analysis\n");
    printf("1. Show all the data\n");
    printf("2. Calculate the average for an experiment\n");
    printf("3. Calculate the average across all experiments\n");
    printf("4. QUIT\n");
    printf("Selection: ");
    scanf("%d", &input);

    switch (input) {
        case 1:
            if(argc!=0){
                displayAll(data, experiments);
            }
            else{
                printf("Oops, something went wrong!\n");
            }
            break;
        case 2:
            if(argc!=0){
                printf("\n");
                printf("Enter the name of the experiment: ");
                scanf("%[^\n]", name);
                individualAverage(name, experiments, data);
            }
            else{
                printf("Oops, something went wrong!\n");
            }
            break;
        case 3:
            if(argc!=0){
                allAverage(experiments, data);
            }
            else{
                printf("Oops, something went wrong!\n");
            }
            break;
        case 4:b=1;;
            break;

        default:
            printf("Oops, something went wrong!\n");
            break;
    }
}
return 0;

}

Thats the beginning of main, which calls this function:

void sendToArray(int data[10][20], char *experiments[20]){
char line[100];
char line2[100];


char *temp1;
int temp;
int i=0, c=0;
while(!strcmp(line, "*** END ***")){
    scanf("%[^\n]", line);
    scanf("%*c");
    scanf("%[^\n]", line2);
    scanf("%*c");
    for(i=0; i<10; i++){

        temp1 = strtok(line2, " ");
        temp = atoi(temp1);
        data[c][i] = temp;
    }
    experiments[c] = line;
    c++;

}
numExperiments = c+1;


}

I changed the function to allocate 100 bits of memory to each line, however I still get the core dump. Also, if I do redirect a text file, the program loops through main infinitely. Thanks if anyone can help me find my mistake!

Here is my sample input file!, which is called in bash by ./dataset < textfile :

Experiment One
3 10 8 7 3 2 9 7 5 6
Experiment Two
10 20 30 40 50 60 70 80 90 100
Control Group
5 5 5 5 5 5 5 5 5 5
*** END ***

Okay, thank you to everyone, I fixed the memory dump, however, whenever I pipe the text file the program still loops infinitely.

Was it helpful?

Solution

The problem is you never allocate memory for line and line2. You could declare them as proper arrays, e.g.:

char line[32];
char line2[32];

This is what is causing the segmentation fault, but fixing that you would have another error: you are passing local addresses to the experiments array. You can fix that either by allocating the line arrays via malloc, or by allocating proper memory for experiments and then copying each string in line with strcpy or something. Also, I am pretty sure that

scanf("%[^\n]\n", line);

is invalid. What you want is

scanf("%[^\n]", line);
scanf("%*c");

OTHER TIPS

You reference data[c][i] without checking that c<20. That's highly unsafe and can result in an illegal memory access (a segfault).

Try restricting c to be lower than 20. Do you still get the error?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top