Why cast to (char *) before de-allocating memory in "Numerical Recipes in C" ?

Hi,

The following code has been taken from Appendix B of the book

"Numerical Recipes in C". The code give a function to perform

the dynamic allocation of a matrix of double with row index

from 'nrl' to 'nrh' and column index from 'ncl' to 'nch' where

the number of rows = nrh - nrl + 1 and

the number of column = nch - ncl + 1.

This code performs very well and I did not encountered a problem with it;

however, I would like to have an explanation or the reason why in

the function to free the allocated memory, 'free_vector' below,

the authors "Numerical Recipes in C" choose to cast the pointers

to (char *) before the memory is de-allocated. Is this necessary ?

Thanks in advance for your help.

Note: The same is done for other numerical objects: vector of doubles,

vector of floats,

vector of integers, matrix of floats, matrix of integers, etc ...

double** dmatrix(int nrl, int nrh, int ncl, int nch)

{

int i,nrow,ncol;

double** m;

nrow = nrh - nrl + 1;

ncol = nch - ncl + 1;

m = (double **) malloc((size_t)((nrow+1)*sizeof(double*)));

m += 1;

m -= nrl;

m[nrl] = (double *) malloc((size_t)((nrow*ncol+1)*sizeof(double)));

m[nrl] += 1;

m[nrl] -= ncl;

for(i=nrl+1; i<=nrh;i++) m[i] = m[i-1] + ncol;

return m;

}

void free_dmatrix(double** m, int nrl,int ncl)

{

free((char*)(m[nrl]+ncl-1));

free((char*)(m+nrl-1));

}

--

Ren Girard

--