Tuesday 9 February 2010

2d dynamic array to be passed in a C function

To allocate it:

float **out=(float **)malloc(nRows * sizeof(float *));
for(unsigned int i = 0; i < nRows; i++)
out[i]=(float *)malloc(nCols * sizeof(float));

To free it:

for(unsigned int i = 0; i < nRows; i++)
free(out[i]);
free(out);

No comments: