im trying to make LU factorization on n x n(getting n from scanf) matrix HEAP CORRUPTION causes when i try to make n x n matrix and put numbers in :/ i dont know where to fix as i am using malloc for the first time
#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;
}