简体   繁体   中英

Heap Corruption while getting LU factorization's nxn matrix

im trying to make LU factorization on nxn(getting n from scanf) matrix HEAP CORRUPTION causes when i try to make nxn 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;

}

A warning is printed as i compiled your code by 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]

I was able to reproduce the heap corruption by using a size of 6.

The correct way to ask for an integer, since matrix is of type int** is :

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

Once this is corrected, the heap corruption seems to be solved. Here is the resulting code. Notice that the return value of malloc() is checked and x is not used as the size of the matrix anymore. Compile it by 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;

}

Hope it helps !

EDIT : here is a link to an interesting question about memory allocation of 2D array . Yours is correct and allows to change the length of each line independently. The other alternative is to allocate all values at once and values are contiguous in memory. This is required by libraries such as lapack of fftw.

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