C 获取LU因子分解时堆损坏';s-nxn矩阵
我正在尝试对nxn(从scanf得到n)矩阵进行LU分解 当我尝试生成n x n矩阵并输入数字时,堆损坏会导致:/ 我不知道在哪里修复,因为我是第一次使用mallocC 获取LU因子分解时堆损坏';s-nxn矩阵,c,heap,factorization,C,Heap,Factorization,我正在尝试对nxn(从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; //d
#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;
}
#包括
#包括
#杂注警告(禁用:4996)
//空洞高斯(矩阵);
内部主(空)
{
inti,n;
int x,y;
int**matrix;//定义矩阵[x][y]
国际**L;
国际**U;
printf(“nxn矩阵类型n.\n”);
scanf(“%d”和&x);
y=x,n=x;
矩阵=(int**)malloc(sizeof(int*)*x);//int*数x主结构
对于(i=0;i当我通过gcc main.c-o main-Wall编译代码时,会打印一条警告:
main.c:51:9:注意:格式“%lf”要求参数类型为“double*”,但参数2的类型为“int*”[-Wformat]
通过使用6的大小,我能够复制堆损坏
由于matrix
属于int**
类型,因此请求整数的正确方法是:
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;
}
#包括
#包括
//空洞高斯(矩阵);
内部主(空)
{
inti,n;
int x,y;
int**matrix;//定义矩阵[x][y]
国际**L;
国际**U;
printf(“nxn矩阵类型n.\n”);
scanf(“%d”和“&n”);
//y=x,n=x;
矩阵=malloc(sizeof(int*)*n);//int*数x主结构
如果(矩阵==NULL){printf(“malloc失败\n”);退出(1);}
对于(i=0;i有多少是x
?如果矩阵太大,可能没有足够的内存来存储它,并且malloc
可能会失败。如果malloc()
失败,它返回NULL
。malloc()
的典型用法是int*bla=malloc(42*sizeof(int));如果(bla==NULL){printf(“无法分配内存”);退出(1);}
混合x
作为循环索引,而n
作为矩阵的大小则不太实际。例如,当数组被释放时,如何确保x
等于n
?好吧……我将x键入4和5,并且都出现了堆损坏:/+我应该如何在不使用n为矩阵输入数字的情况下循环x?:/?为什么要使用指针向量而不是真正的2D矩阵?这种方法可能会出现很多问题。只需执行int(*bla)[y]=malloc(sizeof(int[x][y]))
并且您已经决定了分配。永远不要使用强制转换,或者至少在您对指针更熟悉一点之前不要使用强制转换。它们可能会隐藏许多编译器会告诉您的事情。正如Jens Gustedt所说,不要使用强制转换我可以替换///matrix=malloc(sizeof(int*)*x);例如(i=0;iHo!我忘了这个!@JensGustedt是对的:!建议不要强制转换malloc()的返回值。