简体   繁体   中英

C Segmentation fault with fscanf

Trying to read an input .txt file with using fscanf, and store the line content to the int variable, array, and 2D array, so I can use the value to do computation later. I think the problem over here is because I did not handle the "EOF" with using fscanf?

Here is my code:

int main(){
FILE *fp;

int n;  // # resources
int m;  // # processes
int avail[n];
int max[m][n], allo[m][n];
char temp1[10];

fp = fopen("test.txt", "r");
if (fp == NULL){
    exit(EXIT_FAILURE);
}


fscanf(fp, "%d %d", &n, &m);
printf("%d %d", n, m);
printf("\n");

// Store the second line content to allo[]
for(int i = 0; i < n; i++){
    fscanf(fp, "%s", temp1);
    avail[i] = atoi(temp1);
    printf("%d ", avail[i]);
}
printf("\n");

// Store the line3-7 content to 2D max[][]
for(int i = 0; i < m; i++){
    for(int j = 0; j < n; j++){
        fscanf(fp, "%s", temp1);
        max[i][j] = atoi(temp1);
        printf("%d ", max[i][j]);
    }
    printf("\n");
}

// Store the line8-12 content to 2D allo
for(int i = 0; i < m; i++){
        for(int j = 0; i < n; j++){
            fscanf(fp, "%s", temp1);
            allo[i][j] = atoi(temp1);
            printf("%d ", allo[i][j]);
        }
        printf("\n");
}


fclose(fp);
return 0;
}

Here is the .txt input file:

3 5
9 6 3
5 5 2
4 1 3
8 3 4
5 4 2
4 4 3
0 1 0
1 1 0
1 0 2
0 0 1
1 2 2

And here is the output:

3 5
9 6 3
5 5 2
4 1 3
8 3 4
5 4 2
4 4 3
Segmentation fault: 11

The problem is here:

int n;  // # resources
int m;  // # processes
int avail[n];
int max[m][n], allo[m][n], need[n][m];

n and m are not initialized when you declare the 2D array max . Try printing n and m before int max[m][n]; , etc. and you will see that they contain garbage values.

As a result, Undefined Behavior is what you are experiencing, since you can't really tell what the size of your array is.

Change it to this:

int n;  // # resources
int m;  // # processes

fscanf(fp, "%d %d", &n, &m);
printf("%d %d", n, m);

int avail[n];
int max[m][n], allo[m][n], need[n][m];

Now when you create your arrays, n and m will be initialized with the values read from the file.


If you want to declare your arrays before reading n and m , then you should use pointers, read n and m and then dynamically allocate the arrays .

You declare int max[m][n], allo[m][n], need[n][m] while m and n are not set yet, so they are of unknown size. They will not resize themselves after the sizes are set. So writing to these will give you "undefined behaviour"

Since m,n is not initialized when you declared your arrays at the top of your file:

int avail[n];
int max[m][n], allo[m][n], need[n][m];

as @WeatherVane stated in the comments, you will get undefined behavior. Maybe you are overwriting some other part of the programs memory (who knows it is undefined!).

If you need dynamic array creation you should do something like the following:

int* avail;
...
fscanf(fp, "%d %d", &n, &m);
avail = (int*)malloc(n*sizeof(int));

When you initialize max,allo and need. the value of n and m is not defined.

You can define max , allo and need as int** .

After you have scanned the value of n and m, you can invoke the below function to allocate memory for above 2D arrays.

int ** get2DintArray(int r, int c){
    int **arr = (int **)malloc(r * sizeof(int *));
    for (i=0; i<r; i++)
         arr[i] = (int *)malloc(c * sizeof(int));
    return arr;
}

For. eg:-

allo = get2DintArray(m,n);
need = get2DintArray(n,m);

This approach will be handy for higher values of n and m, where the stack memory may not be sufficient, because you are using heap memory in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM