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

C代码中Segmentation fault (core dumped)错误原因排查及修复方案咨询

Hey there! Let's break down exactly why you're seeing that Segmentation fault (core dumped) error and walk through how to fix your code.

Why You're Getting the Segmentation Fault & How to Fix It

Root Cause

Your dgemm function is built to handle n×n 2D arrays of doubles—it uses indexing like i + k * n to traverse contiguous blocks of memory that hold n² double values each.

But in your main function, you only declared single double variables (A, B, C) and passed their addresses to dgemm. When you call dgemm(40, &A, &B, &C), the function tries to access memory far beyond the single double each pointer points to. For example, when i=0 and k=1, the index becomes 0 + 1*40 = 40, meaning the function tries to read the 41st double starting from &A. That memory doesn't belong to your program, so the operating system kills it to prevent memory corruption—hence the segmentation fault.

Fixes with Code Examples

You need to create actual arrays large enough to hold 40×40 doubles (1600 elements each). Here are two reliable ways to do this:

Option 1: Static Arrays (For Smaller n)

If n is small enough (40 is totally fine here), you can declare static arrays on the stack:

#include <stdio.h>

void dgemm (int n, double *A, double *B, double *C) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            double cij = C[i + j * n];
            for (int k = 0; k < n; k++) {
                cij += A[i + k * n] * B[k + j * n];
            }
            C[i + j * n] = cij;
        }
    }
}

int main() {
    const int n = 40;
    // Declare 1D arrays to simulate 2D matrices (40x40 = 1600 elements each)
    double A[n*n];
    double B[n*n];
    double C[n*n];

    // Initialize arrays with your test values
    for (int i = 0; i < n*n; ++i) {
        A[i] = 4.1;
        B[i] = 5.2;
        C[i] = 7.4;
    }

    // Pass array names directly (they decay to pointers to the first element)
    dgemm(n, A, B, C);

    // Verify the result (print the first element of C as an example)
    printf("C[0][0] = %f\n", C[0]);

    return 0;
}

Option 2: Dynamic Memory Allocation (For Larger n)

If you might use a larger n later (where stack space could be insufficient), use dynamic allocation with malloc and free:

#include <stdio.h>
#include <stdlib.h>

void dgemm (int n, double *A, double *B, double *C) {
    // Function logic remains the same
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            double cij = C[i + j * n];
            for (int k = 0; k < n; k++) {
                cij += A[i + k * n] * B[k + j * n];
            }
            C[i + j * n] = cij;
        }
    }
}

int main() {
    const int n = 40;
    // Allocate memory for 40x40 matrices
    double *A = malloc(n*n * sizeof(double));
    double *B = malloc(n*n * sizeof(double));
    double *C = malloc(n*n * sizeof(double));

    // Check if allocation succeeded (critical to avoid crashes)
    if (!A || !B || !C) {
        fprintf(stderr, "Failed to allocate memory!\n");
        return 1;
    }

    // Initialize array values
    for (int i = 0; i < n*n; ++i) {
        A[i] = 4.1;
        B[i] = 5.2;
        C[i] = 7.4;
    }

    dgemm(n, A, B, C);

    printf("C[0][0] = %f\n", C[0]);

    // Don't forget to free the allocated memory!
    free(A);
    free(B);
    free(C);

    return 0;
}

Quick Recap

A segmentation fault almost always means your program is accessing memory it doesn't own. In this case, it's because you passed pointers to single doubles where the function expected pointers to large arrays. Fixing it just requires giving the function the properly sized memory it needs.

内容的提问来源于stack exchange,提问作者Jesus Meza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 14:02:32