获取 LU 分解的 nxn 矩阵时堆损坏

Heap Corruption while getting LU factorization's nxn matrix

我正在尝试对 n x n(从 scanf 获取 n)矩阵进行 LU 分解 当我尝试制作 n x n 矩阵并将数字放入:/ 我不知道在哪里修复,因为我是第一次使用 malloc

#include <stdio.h> 

#include <stdlib.h>

#pragma warning(disable:4996)

//void gauss(matrix);

int main(void)

{

int i, n;

int x, y;

int **matrix; //define matrix[x][y]

int **L;

int **U;

printf("nxn matrix type n.\n");

scanf("%d", &x);

y = x, n = x;

matrix = (int **)malloc(sizeof(int *) * x); // int* number x primary structure
for (i = 0; i<x; i++)
{
    matrix[i] = (int *)malloc(sizeof(int) * y);
} //build matrix[x][y(size of x)] structure

L = (int **)malloc(sizeof(int *) * x); // int* number x primary structure
for (i = 0; i<x; i++)
{
    L[i] = (int *)malloc(sizeof(int) * y);
} //build L[x][y(size of x)] structure

U = (int **)malloc(sizeof(int *) * x); // int* number x primary structure
for (i = 0; i<x; i++)
{
    U[i] = (int *)malloc(sizeof(int) * y);
} //build U[x][y(size of x)] structure

printf("type the number of matrix \n");
for (x = 0; x < n; x++){
    for (y = 0; y < n; y++){
        printf("line %d  x%d number : ", x + 1, y + 1);
        scanf("%lf", &matrix[x][y]);
    }
}




for (i = 0; i<x; i++)
{
    free(matrix[i]);
}
free(matrix);//free matrix

for (i = 0; i<x; i++)
{
    free(L[i]);
}
free(L);//free L

for (i = 0; i<x; i++)
{
    free(U[i]);
}
free(U);//free U

return 0;

}

当我通过 gcc main.c -o main -Wall 编译您的代码时打印了一条警告:

main.c:51:9: attention : format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘int *’ [-Wformat]

我能够通过使用大小 6 重现堆损坏。

请求整数的正确方法,因为 matrix 属于 int** 类型:

scanf("%d", &matrix[x][y]);

更正此问题后,堆损坏问题似乎就解决了。 这是结果代码。请注意,malloc() 的 return 值已被选中,并且 x 不再用作矩阵的大小。按gcc main.c -o main

编译
#include <stdio.h> 

#include <stdlib.h>


//void gauss(matrix);

int main(void)

{

    int i, n;

    int x, y;

    int **matrix; //define matrix[x][y]

    int **L;

    int **U;

    printf("nxn matrix type n.\n");

    scanf("%d", &n);

    //y = x, n = x;

    matrix = malloc(sizeof(int *) * n); // int* number x primary structure
    if(matrix==NULL){printf("malloc failed\n");exit(1);}
    for (i = 0; i<n; i++)
    {
        matrix[i] = malloc(sizeof(int) * n);
        if(matrix[i]==NULL){printf("malloc failed\n");exit(1);}
    } //build matrix[x][y(size of x)] structure

    L = malloc(sizeof(int *) * n); // int* number x primary structure
    if(L==NULL){printf("malloc failed\n");exit(1);}
    for (i = 0; i<n; i++)
    {
        L[i] = malloc(sizeof(int) * n);
        if(L[i]==NULL){printf("malloc failed\n");exit(1);}
    } //build L[x][y(size of x)] structure

    U = malloc(sizeof(int *) * n); // int* number x primary structure
    if(U==NULL){printf("malloc failed\n");exit(1);}
    for (i = 0; i<n; i++)
    {
        U[i] = malloc(sizeof(int) * n);
        if(U[i]==NULL){printf("malloc failed\n");exit(1);}

    } //build U[x][y(size of x)] structure

    printf("type the number of matrix \n");
    for (x = 0; x < n; x++){
        for (y = 0; y < n; y++){
            printf("line %d  x%d number : ", x + 1, y + 1);
            scanf("%d", &matrix[x][y]);
        }
    }




    for (i = 0; i<n; i++)
    {
        free(matrix[i]);
    }
    free(matrix);//free matrix

    for (i = 0; i<n; i++)
    {
        free(L[i]);
    }
    free(L);//free L

    for (i = 0; i<n; i++)
    {
        free(U[i]);
    }
    free(U);//free U

    return 0;

}

希望对您有所帮助!

编辑:这是一个 link 关于一个有趣的问题 memory allocation of 2D array。你的是正确的,允许独立改变每行的长度。另一种选择是一次分配所有值,并且值在内存中是连续的。这是fftw的lapack等库所需要的。