gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:24 27 4
gpt4 key购买 nike

我正在尝试对 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 的大小来重现堆损坏。

请求整数的正确方法,因为 matrixint** 类型:

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

一旦这个问题得到纠正,堆损坏问题似乎就解决了。这是结果代码。请注意,检查了 malloc() 的返回值,并且 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;

}

希望对您有所帮助!

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

关于c - 获取 LU 分解的 nxn 矩阵时堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com