1

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;

}
trincot
  • 317,000
  • 35
  • 244
  • 286
Iku Kim
  • 13
  • 5
  • How much is `x` ? If the matrix is too big, there might be not enough memory to store it and `malloc` can fail. If `malloc()` fails, it returns `NULL`. Typical usage of `malloc()` is `int* bla= malloc(42*sizeof(int));if(bla==NULL){printf("unable to allocate memory\n");exit(1);}` – francis Apr 26 '15 at 14:59
  • Mixing `x` as a loop index and `n` as the size of the matrix is not so practical. For instance, as the arrays are freed, how do you ensure that `x` is equal to `n` ? – francis Apr 26 '15 at 15:03
  • well.... i typed x as 4 and 5 and all did got Heap corruption :/ – Iku Kim Apr 26 '15 at 15:22
  • + how should i loop x with out using n to input numbers for matrix? :/? – Iku Kim Apr 26 '15 at 15:23
  • Why do you use a vector of pointers instead of a real 2D matrix? There are many things that can go wrong with such an approach. Simply do `int (*bla)[y] = malloc(sizeof(int[x][y]));` and you are settled for the allocation. And don't use casts, ever, or at least before you are a bit more familiar with pointers. They may hide a lot of things that otherwise the compiler would tell you. – Jens Gustedt Apr 26 '15 at 15:26
  • hum...... so if i want to use n x n matrix should i use like //// matrix = malloc(sizeof(int*)*x); for (i = 0; i – Iku Kim Apr 26 '15 at 15:38

1 Answers1

1

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.

Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41
  • as Jens Gustedt said do not use casts can i replace////matrix = malloc(sizeof(int*)*x); for (i = 0; i – Iku Kim Apr 26 '15 at 15:47
  • Ho ! I forgot this one ! @JensGustedt is right : [Do not cast the return of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) ! It is advised not to cast the return value of `malloc()` . – francis Apr 26 '15 at 15:48