For an interview, I was asked to write code allocate memory for a rows*cols matrix, using a single malloc()
. I want your comments about this code that I wrote:
/*
Allots (in effect) space for a matrix of size rows*cols using a single malloc.
The whole space allotted consists of an array of size 'rows' of float ptrs,
followed by a float array of size 'rows' * 'cols'.
The float ptrs 1,2.., are assigned to row no. 1, 2,.. in the matrix that follows.
*/
float** allotMatrix(const size_t &rows, const size_t &cols)
{
// Compute size of allotment = rows * cols floats + rows float ptrs
size_t ptrsize = rows * sizeof(float*);
size_t datasize = rows * cols * sizeof(float);
size_t matsize = ptrsize + datasize;
// Get if possible a memory allotment of this size. We use char* for convenience
char* cmatrix = (char*) malloc(matsize);
if (cmatrix == NULL)
{
fprintf(stderr, "Unable to allot memory");
exit(1);
}
memset((void*) cmatrix, 0, matsize);
// Assign ptrs to data
float** ptrstart = (float**) cmatrix;
float* datastart = (float*) (cmatrix + ptrsize);
for (size_t i = 0; i != rows; ++i)
{
*ptrstart = datastart;
// Move to next ptr
++ptrstart;
// .. and move to the next data row
datastart += cols;
}
return (float**) cmatrix;
}