简体   繁体   English

C 分段错误 fscanf

[英]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.尝试使用 fscanf 读取输入的 .txt 文件,并将行内容存储到 int 变量、数组和二维数组中,以便稍后使用该值进行计算。 I think the problem over here is because I did not handle the "EOF" with using fscanf?我认为这里的问题是因为我没有使用 fscanf 处理“EOF”?

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:这是 .txt 输入文件:

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 .当您声明二维数组max时, nm初始化。 Try printing n and m before int max[m][n];尝试在int max[m][n];之前打印nm 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.现在,当您创建数组时, nm将使用从文件中读取的值进行初始化。


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 .如果你想在读取nm之前声明你的数组,那么你应该使用指针,读取nm然后动态分配数组

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.您声明int max[m][n], allo[m][n], need[n][m]mn尚未设置,因此它们的大小未知。 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:由于 m,n 在文件顶部声明数组时未初始化:

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

as @WeatherVane stated in the comments, you will get undefined behavior.正如@WeatherVane 在评论中所述,您将获得未定义的行为。 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.当您初始化 max、allo 和需要时。 the value of n and m is not defined. n 和 m 的值没有定义。

You can define max , allo and need as int** .您可以将maxalloneed定义为int**

After you have scanned the value of n and m, you can invoke the below function to allocate memory for above 2D arrays.扫描 n 和 m 的值后,您可以调用以下函数为上述二维数组分配内存。

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.这种方法对于更高的 n 和 m 值很方便,其中堆栈内存可能不够用,因为在这种情况下您使用的是堆内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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