简体   繁体   中英

segmentation fault in dynamic allocation of a 2D array

this code worked fine for n=10,000 but for n=100,000 on a machine with 2GB ram. kswap0 was called for n=10,000 on a machine with 1GB ram but immediately showed segmentation fault for n=100,000.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int **createMatrix(int n)
{
    int **mat=(int**)malloc(n*sizeof(int*));
    int i;
    for(i=0;i<n;i++)
    {
        mat[i]=(int*)malloc(n*sizeof(int));
    }
    return mat;
}
void display(int **mat, int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",mat[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    int n=100000;
    int **matrixOne=createMatrix(n);
    int **matrixTwo=createMatrix(n);
    int **resultantMatrix=createMatrix(n);
    srand(time(NULL));
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            matrixOne[i][j]=rand()%10;
            matrixTwo[i][j]=rand()%10;
        }
    }
    display(matrixOne,n);
    display(matrixTwo,n);
    int k;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                resultantMatrix[i][j]+=matrixOne[i][k]*matrixTwo[k][j];
            }
        }
    }
    display(resultantMatrix,n);
    for(i=0;i<n;i++)
    {
        free(matrixOne[i]);
        free(matrixTwo[i]);
        free(resultantMatrix[i]);
    }

Thank you in advance!

An int is 4 bytes. In createMatrix, ignoring the first malloc, you're allocating n * n * sizeof(int) bytes. For n=100,000, this is 40,000,000,000 bytes, or about 40 GB. Since you're doing this 3 times, you'd need about 120 GB of RAM, which you don't have. For n = 10,000, you only need about 1.2 GB, which you do have (including swap space).

As the comments mentioned, you should check the result of malloc to get a clearer error message, and avoid the seg fault.

I can not allocate memory, because matrix is too big for my RAM. Check result of malloc every time

int **createMatrix(int n) {
    int **mat = NULL;
    int i;

    mat = malloc(n*sizeof(int*));
    if (mat == NULL) {
        exit(1);
    }
    for (i = 0; i<n; i++) {
        mat[i] = malloc(n*sizeof(int));
        if (mat[i] == NULL) {
            exit(2);
        }
    }
    return mat;
}

This approach is not optimal for memory use because you use more RAM than necessary. You create a matrix as an array of array, so for each row you allocate you have a memory overhead:

  • The first 1D array you allocate don't contains data but pointers.
  • The C runtime library will creates heap meta-data

While this is kind of nice because you can write M[i][j] like a static 2D array, you will also have much slower allocation (and deallcoation) than a traditional 1D array with row-major or column-major indexing:

//allocation:
int * M = malloc(nCol * nRow * sizeof(int));

//access:
M[i + nRow*j] = data; // Column major
M[i*nCol + j] = data; // Row major

//deallocation:
free(M);

http://en.wikipedia.org/wiki/Row-major_order

Finally, data access implies a double dereference that is likely to be slower that row-major or column-major indexing.

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