Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D Dynamic Array Allocation and Passing by Reference in C

Can someone wiser than I please explain to me why the following code segment faults? There is no problem allocating the memory by reference, but as soon as I try to assign anything or free by reference, segfault occurs.

I'm sure I'm missing some fundamental concept about pointers and passing by reference, hopefully some light can be shed.

#include <stdlib.h>
#include <stdio.h>

void allocateMatrix(float ***);
void fillMatrix(float ***);
void freeMatrix(float **);

int main() {
    float **matrix;

    allocateMatrix(&matrix);        // this function calls and returns OK
    fillMatrix(&matrix);            // this function will segfault
    freeMatrix(matrix);             // this function will segfault

    exit(0);
}

void allocateMatrix(float ***m) {
    int i;
    m = malloc(2*sizeof(float*));
    for (i = 0; i < 2; i++) {
        m[i] = malloc(2*sizeof(float));
    }
    return;
}

void fillMatrix(float ***m) {
    int i,j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            (*m)[i][j] = 1.0;        // SEGFAULT
        }
    }
    return;
}

void freeMatrix(float **m) {
    int i; 
    for (i = 0; i < 2; i++) {
        free(m[i]);                  // SEGFAULT
    }
    free(m);
    return;
}
like image 785
holocron Avatar asked Apr 07 '12 04:04

holocron


People also ask

Can we allocate a 2 dimensional array dynamically?

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements.

Can you pass a 2D array in C?

A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.


2 Answers

One set of problems is here:

void allocateMatrix(float ***m) {
    int i;
    m = malloc(2*sizeof(float*));
    for (i = 0; i < 2; i++) {
        m[i] = malloc(2*sizeof(float));
    }
    return;
}

You need to assign to *m to get the information back to the calling code, and also you will need to allocate to (*m)[i] in the loop.

void allocateMatrix(float ***m)
{
    *m = malloc(2*sizeof(float*));
    for (int i = 0; i < 2; i++)
        (*m)[i] = malloc(2*sizeof(float));
}

There's at least a chance that the other functions are OK. The fillMatrix() is written and invoked correctly, though it could be simplified by losing the third * from the pointer:

void fillMatrix(float **m)
{
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
            m[i][j] = 1.0;        
    }
}

It might be advisable to pass the triple-pointer to freeMatrix() so that you can zero the pointer in the calling function:

void freeMatrix(float ***m)
{
    for (int i = 0; i < 2; i++)
        free((*m)[i]);
    free(*m);
    *m = 0;
}

Calling then becomes:

allocateMatrix(&matrix);
fillMatrix(matrix);
freeMatrix(&matrix);   
like image 132
Jonathan Leffler Avatar answered Oct 25 '22 10:10

Jonathan Leffler


Good use of indirection. Just try to be consistent with format. It improves readability and reduces errors. e.g.

function calls:

    allocateMatrix  &matrix
    fillMatrix  &matrix
    freeMatrix  &matrix

declarations

void allocateMatrix  float ***m
void fillMatrix  float ***m
void freeMatrix  float ***m

handling

    (*m)[i] = malloc(2 * sizeof(float))
    (*m)[i][j] = 1.0
    free  (*m)[i]
like image 32
hellork Avatar answered Oct 25 '22 09:10

hellork