You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言使用指针实现两矩阵乘法时触发段错误问题求助

问题定位与修复方案

核心错误点

  • 野指针未分配内存:main函数中定义的ap、bp、cp三个float指针没有申请对应的堆内存空间,直接传入读写函数操作非法内存,是触发段错误的直接原因。
  • 一维数组模拟二维矩阵的寻址逻辑错误:原代码中访问[i][j]位置元素的写法*(arrp + i) + j完全错误,正确的寻址公式应为arrp + i * 列数 + j,需要按每行的元素总数计算行偏移量。

修复后完整代码

#include<stdio.h>
#include<stdlib.h>
#define N 10

void readMatrix(float *arrp, int rows, int cols)
{
  for (int i=0; i<rows; i++)
  {
    for (int j=0; j<cols; j++)
    {
        printf("Enter element [%d][%d] : ",i+1,j+1);
        scanf("%f", arrp + i * cols + j);
    }
  }
  return;
}

void printMatrix(float *arrp, int rows, int cols)
{
  for (int i=0; i<rows; i++)
  {
    for (int j=0; j<cols; j++) 
        printf("%.2f\t", *(arrp + i * cols + j));
    printf("\n");
  }
  return;
}

int main()
{
    float *ap, *bp, *cp;
    int arows, brows, acols, bcols;

    printf("Give no. of rows of matrix A (<=%d) = ",N);
    scanf("%d",&arows);
    printf("Give no. of columns of matrix A (<=%d) = ",N);
    scanf("%d",&acols);
    if (arows>N || acols>N || arows<1 || acols<1)
    {
        printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", arows, acols, N);
        exit(0);
    }
    // 给矩阵A分配内存
    ap = (float*)malloc(sizeof(float) * arows * acols);
    readMatrix(ap, arows, acols); 
    printf("\n");
    printf("Matrix A :\n"); 
    printMatrix(ap, arows, acols); 
    printf("\n");

    printf("Give no. of rows of matrix B (<=%d) = ", N);
    scanf("%d",&brows);
    printf("Give no. of columns of matrix B (<=%d) = ", N);
    scanf("%d",&bcols);
    if (brows>N || bcols>N || brows<1 || bcols<1)
    {
        printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", brows, bcols, N);
        // 退出前释放已分配的内存
        free(ap);
        exit(0);
    }

    if (acols==brows)
    {
        // 给矩阵B分配内存
        bp = (float*)malloc(sizeof(float) * brows * bcols);
        readMatrix(bp, brows, bcols); 
        printf("\n");
        printf("Matrix B :\n"); 
        printMatrix(bp, brows, bcols); 
        printf("\n");

        // 给结果矩阵C分配内存:行数=A的行数,列数=B的列数
        cp = (float*)malloc(sizeof(float) * arows * bcols);
        for (int i=0; i<arows; i++)
        {
            for (int j=0; j<bcols; j++)
            {
              float sum=0.0;
              for (int k=0; k<acols; k++) 
                sum += *(ap + i * acols + k) * *(bp + k * bcols + j);
              *(cp + i * bcols + j) = sum;
            }
        }

        printf("After Multiplication, Matrix C :\n"); 
        printMatrix(cp, arows, bcols);

        // 释放所有堆内存
        free(ap);
        free(bp);
        free(cp);
    }
    else {
        printf("\nIncompatible matrices\nColumns of matrix A should be equal to rows of matrix B\n");
        free(ap);
    }
    exit(0);
}

额外优化说明

  • 补充了所有申请内存的释放逻辑,避免内存泄漏
  • 修正了原代码中换行符缺失、制表符转义错误的问题
  • 各分支退出前都先释放已分配的内存,符合C语言内存管理规范

内容的提问来源于stack exchange,提问作者Sparsh Arora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 04:06:04