【问题标题】:segmentation fault on simple loop简单循环上的分段错误
【发布时间】:2021-09-19 10:24:25
【问题描述】:

我正在尝试编写一个程序来计算给定矩阵的元素数,例如 M[2][3] = [1,2,3][3,4,5] 应该给出 nxm 数组中从 0 到 9 的元素数。 该算法在我遇到分段错误的第 34 行(M[i][j] = i + 2;) 之前都是正确的。 我做错了什么?

#include<stdio.h>
#include<stdlib.h>
#define ROW 10
#define COL 10
#define MAX 10
void Print_Matrix(int **M,int row,int col);
int MallocX(int **M,int row,int col);
int main(void)
{
    int **M = 0x0;
    int count[MAX] = {0};
    int i,j;
    
    
    
    if(MallocX(M,ROW,COL)){
        fprintf(stdout,"Could not allocate memory\n");
        exit(1);
    }

    for(i = 0;i<ROW;i++){
        for(j = 0;j<COL;j++){
            M[i][j] = i + 2;
        }
    }
    Print_Matrix(M,ROW,COL);
    for(i = 0;i<ROW;i++){
        for(j = 0;j<COL;j++){
            ++count[M[i][j]];
        }
    }
    
    for(j = 0;j<MAX;j++){
        if(count[j]){
            printf("%d %d\n",j,count[j]);
        }
    }
    for(i = 0;i<ROW;i++){
        free(M[i]);
    }
    free(M);
}
int MallocX(int **M,int row,int col)
{
    int i;
    M = (int **) malloc(row * sizeof(int *));
    if(M == NULL){
        fprintf(stderr,"Error allocating memory\n");
        free(M);
        return 1;
    }

    for(i = 0;i<row;i++){   
        M[i] = (int *) malloc(col * sizeof(int));
        if(M[i] == NULL){
            fprintf(stderr,"Error allocating memory\n");
            free(M[i]);
            return 1;
        }
    }
    return 0;
}
void Print_Matrix(int **M,int row,int col)
{
    int i,j;
    for(i = 0;i<row;i++){
        for(j = 0;j<col;j++){
            printf("%d ",M[i][j]);
        }
        printf("\n");
    }
}

【问题讨论】:

  • 你写到了count 的末尾。 count 中的最大有效索引是 MAX-1,即 9。M 中的最大值是 9+2 = 11。所以你在末尾写了两个元素 ++count[M[i][j]]
  • 但它在 (M[i][j] = i + 2) 上给出了段错误
  • 好的。我会试试的。
  • 仍然给我段错误。
  • C 中的所有参数都是按值传递的。您可以通过显式传递变量的地址来模拟按引用传递,但您没有这样做。所以,这是你的主要错误。

标签: c matrix segmentation-fault


【解决方案1】:

这是因为您通过值而不是引用传递 M。您的MallocX 为您的矩阵分配内存,但是当您返回主程序时,这些分配是孤立的,其中M 仍然是0x0(或NULL),这就是分配函数通常返回指针的原因。也许你想要这样的东西:

int **MallocX(int row,int col)
{
    int **Matrix, i;

    Matrix = malloc(row * sizeof(int*));
    if(Matrix == NULL) {
        fprintf(stderr,"Error allocating memory\n");
        return NULL;
    }

    for (i = 0; i < row; i++) {
        Matrix[i] = (int*) malloc(col * sizeof(int));
        if(Matrix[i] == NULL){
            fprintf(stderr,"Error allocating memory (%d)\n",i);
            for (int j = 0; j < i; ++j)
                free(Matrix[j]);
            free(Matrix);
            return NULL;
        }
    }
    return Matrix;
}

然后在main 中调用它:

if (!(M = MallocX(ROW,COL)) {
    fprintf(stdout,"Could not allocate memory\n");
    exit(1);
}

请注意,在您的原始代码中,当 MNULL 时,您调用了 free(M),这本身会导致段错误。所以我也稍微整理了一下你的垃圾收集器。

【讨论】:

  • C 标准允许使用NULL 指针调用free。所以 OP 的 free 调用是无用的,但也是无害的。
  • 我不知道,@user3386109。每天学习新东西!
猜你喜欢
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多