简体   繁体   English

linux。 分段错误(核心转储)

[英]linux. Segmentation fault (core dumped)

I'm trying to solve Gaussian Elimination and Back Substitution in C.我正在尝试解决 C 中的高斯消除和反向替换。 But I've got Segmentation fault(Core dumped) error in shell.但是我在 shell 中出现了分段错误(核心转储)错误。

this is the part of main code.这是主要代码的一部分。

float **a = (float **) malloc(sizeof(float*) *n);
for (int i = 0; i < n; i++)
    a[i] = (float*) malloc(sizeof(float) *n);

float *b = (float*) malloc(sizeof(float) *n);
float *x = (float*) malloc(sizeof(float) *n);
Gaussian(n, &a, &b);
BackSubstitution(n, &a, &b, &x);

and below is gaussian.c.以下是高斯。c。 I think there is some problem with gaussian.c我认为 gaussian.c 有一些问题

#include <math.h>

void Gaussian(int n, float ***arr, float **arr2)
{
    for (int l = 0; l < n - 1; l++)
    {
        for (int i = l + 1, j = 1; i < n && j < n; i++, j++)
        { (*arr)[i][j] = (*arr)[i][j] - ((*arr)[i][l] / (*arr)[l][l]) * (*arr)[l][j];
            (*arr2)[i] = (*arr2)[i] - ((*arr)[i][l] / (*arr)[l][l]) * (*arr2)[l];
        }
    }
}

void BackSubstitution(int n, float ***arr, float **arr2, float **result)
{
    for (int i = n - 1; i > 0; i--)
    {
        (*result)[i] = (*arr2)[i] / (*arr)[i][i];
        for (int j = 0; j < i; j++)
        { (*arr2)[j] = (*arr2)[j] - (*result)[i] * (*arr)[j][i];
            (*arr)[j][i] = 0;
        }
    }
}

Is there something wrong that generate segmentation fault?有什么错误会产生分段错误吗?

A few things:一些东西:

You have no reason to pass your arrays by pointer reference.您没有理由通过指针引用传递您的 arrays 。 So your functions gets much easier by eliminating one extra reference:因此,通过消除一个额外的引用,您的函数会变得更加容易:

void Gaussian(int n, float** arr, float* arr2) {
    for (int l = 0; l < n - 1; l++) {
        for (int i = l + 1, j = 1; i < n && j < n; i++, j++) {
            arr[i][j] = arr[i][j] - arr[i][l] / arr[l][l] * arr[l][j];
            arr2[i] = arr2[i] - arr[i][l] / arr[l][l] * arr2[l];
        }
    }
}

void BackSubstitution(int n, float** arr, float* arr2, float* result) {
    for (int i = n - 1; i > 0; i--) {
        result[i] = arr2[i] / arr[i][i];
        for (int j = 0; j < i; j++) {
            arr2[j] = arr2[j] - result[i] * arr[j][i];
            arr[j][i] = 0;
        }
    }
}

Second, you aren't actually initializing the contents of your arrays with valid data.其次,您实际上并没有使用有效数据初始化 arrays 的内容。 Some of your array initializations are missing initializations to actual floating point data.您的一些数组初始化缺少对实际浮点数据的初始化。 Without this, your arrays have garbage data - which won't play well with floating point.没有这个,你的 arrays 就会有垃圾数据——它不能很好地处理浮点数。

So aside from initializing your arrays correctly, you don't have to pass them in by pointer (because arrays degrade to pointers in function calls)因此,除了正确初始化 arrays 之外,您不必通过指针传递它们(因为 arrays 降级为 function 调用中的指针)

int n = 10;

float** a = (float**)malloc(n * sizeof(float*));
for (int i = 0; i < n; i++)
{
    a[i] = (float*)malloc(n * sizeof(float));
    for (int j = 0; j < n; j++)
    {
        a[i][j] = 0.0f;  // you initialize a[i][j] with your data
    }
}

float* b = (float*)malloc(n * sizeof(float));
float* x = (float*)malloc(n * sizeof(float));
for (int i = 0; i < n; i++)
{
    b[i] = 0.0f;
    x[i] = 0.0f;
}

Gaussian(n, a, b);
BackSubstitution(n, a, b, x);

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

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