Style for handling matrices in C

Dave Rifkind dave at dsi.COM
Mon Oct 16 17:30:01 AEST 1989


In article <7229 at cognos.UUCP> alanm at cognos.UUCP (Alan Myrvold) writes:
>So my question is, what C style SHOULD become commonplace for
>allocating matrices....

Here's one that doesn't require any special action to free up the
matrix:

     double **mat_alloc(row, col)
     {
	  int i;
	  double *dp, **dpp;

	  dpp = malloc(row * sizeof(double *) + row * col * sizeof(double));
	  if (dpp != NULL) {
	       dp = dpp + row;
	       for (i = 0; i < row; i++)
		    dpp[i] = dp + i * col;
	  }
	  return dpp;
     }

This is off the top of my head and may not make sense.  The idea is to
put the pointer array and all of the row arrays in a single allocated
block.  To get rid of it, you just free it.



More information about the Comp.lang.c mailing list