Segmentation Fault Issue in Matrix GDB Project

huangapple go评论57阅读模式
英文:

Segmentation Fault Issue in Matrix GDB Project

问题

我在为课程做一个GDB项目,我在一段读取文件并创建输入矩阵的代码中遇到了段错误的问题。GDB将我带到了printMatrix()中的"printf("%.2d\t", mat[row][col]);",但我似乎无法理解出错的具体原因。

int main(int argc, char* argv[])
{
   FILE* fp = fopen(argv[1], "r");
   int size = 0;
   int **mat = readFile(fp, &size);
   printMatrix(mat, size);
   return 0;
}
int** readFile(FILE* fp, int *size)
{
    fscanf(fp, "%d", size);
    int num = *size;
    int index = 0;
    
    int** mat = (int**)malloc(num * sizeof(int*));
    for(index = 0; index < num; index++)
        mat[index] = (int*)malloc(num * sizeof(int)); 

    int row = 0;
    int col = 0;
    for(; row < num; row++)
    {
        for(; col < num; col++)
        {
            fscanf(fp, "%d", &mat[row][col]);
        }
    }
    return mat;
}


void printMatrix (int** mat, int num)
{
    int row = 0;
    int col = 0;
    for(row = 0; row < num; row++)
    {
        for(col = 0; col < num; col++)
        {
            printf("%.2d\t", mat[row][col]);   /* gdb在这里指示段错误 */
        }
        printf("\n");
    }
}

我尝试调整for循环,但似乎无法获得期望的输出。

英文:

I'm working on a GDB project for class an I'm finding issues with a segfault portion in a piece of code that reads a file and creates a matrix with the input. GDB brings me to the "printf("%.2d\t", mat[row][col]);" in printMatrix() but I can't seem to grasp what exactly goes wrong.

int main(int argc, char* argv[])
{
   FILE* fp = fopen(argv[1], &quot;r&quot;);
   int size = 0;
   int **mat = readFile(fp, &amp;size);
   printMatrix(mat, size);
   return 0;
}
int** readFile(FILE* fp, int *size)
{
    fscanf(fp, &quot;%d&quot;, size);
    int num = *size;
    int index = 0;
    
    int** mat = (int**)malloc(num * sizeof(int));
    for(index = 0; index &lt; num; index++)
        mat[index] = (int*)malloc(num * sizeof(int)); 

    int row = 0;
    int col = 0;
    for(; row &lt; num; row++)
    {
        for(; col &lt; num; col++)
        {
            fscanf(fp, &quot;%d&quot;, &amp;mat[row][col]);
        }
    }
    return mat;
}


void printMatrix (int** mat, int num)
{
    int row = 0;
    int col = 0;
    for(row = 0; row &lt; num; row++)
    {
        for(col = 0; col &lt; num; col++)
        {
            printf(&quot;%.2d\t&quot;, mat[row][col]);   /* gdb indicates segfault here */
        }
        printf(&quot;\n&quot;);
    }
}

I tried tinkering with the for loop but can't seem to get the expected output.

答案1

得分: 0

int** mat = malloc(num * sizeof(int*));

或者更好的写法是

int** mat = malloc(num * sizeof(*mat));

同时,对于大小,请使用 size_t 而不是 int

个人建议使用指向数组的指针,而不是指针数组,以消除一级间接性(难以进行分配/释放,性能损失)。

始终检查 mallocscanf 的结果。

void *readFile(FILE* fp, size_t *size)
{
    if(fscanf(fp, "%zu", size) != 1) { /* 处理错误 */}
    int (*mat)[*size] = malloc(*size * sizeof(*mat));
    if(mat)
    {
        for(size_t row = 0; row < *size; row++)
        {
            for(size_t col = 0; col < *size; col++)
            {
                if(fscanf(fp, "%d", &mat[row][col]) != 1) { /* 处理错误 */}
            }
        }
    }
    return mat;
}

void printMatrix (size_t num, int (*mat)[num])
{
    if(mat)
    {
        for(size_t row = 0; row < num; row++)
        {
            for(size_t col = 0; col < num; col++)
            {
                printf("%.2d\t", mat[row][col]);   /* gdb 在这里指示段错误 */
            }
            printf("\n");
        }
    }
}
英文:
int** mat = (int**)malloc(num * sizeof(int));

It should be

int** mat = malloc(num * sizeof(int *));

or better

int** mat = malloc(num * sizeof(*mat));

Also use site_t for sizes not int.

I personally would use pointer to array not array of pointers to remove one level of indirection (difficult (de)/allocation , performance penalty)

Always check the result of malloc and scanf

void *readFile(FILE* fp, size_t *size)
{
    if(fscanf(fp, &quot;%d&quot;, size) != 1) { /* handle  error */}
    int (*mat)[*size] = malloc(*size * sizeof(*mat));
    if(mat)
    {
        for(size_t row = 0; row &lt; *size; row++)
        {
            for(size_t col = 0; col &lt; *size; col++)
            {
                if(fscanf(fp, &quot;%d&quot;, &amp;mat[row][col]) != 1) { /* handle  error */}
            }
        }
    }
    return mat;
}


void printMatrix (size_t num, int (*mat)[num])
{
    if(mat)
    {
        for(size_t row = 0; row &lt; num; row++)
        {
            for(size_t col = 0; col &lt; num; col++)
            {
                printf(&quot;%.2d\t&quot;, mat[row][col]);   /* gdb indicates segfault here */
            }
            printf(&quot;\n&quot;);
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月16日 05:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定