简体   繁体   中英

linux. Segmentation fault (core dumped)

I'm trying to solve Gaussian Elimination and Back Substitution in C. But I've got Segmentation fault(Core dumped) error in 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. I think there is some problem with 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. 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. 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.

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)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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