Style for handling matrices in C

Harish Hiriyannaiah harish at csl.ncsu.edu
Tue Oct 17 12:01:27 AEST 1989


In article <278 at dsi.COM> dave at dsi.UUCP (Dave Rifkind) writes:
-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;
-     }



	Here you are assuming that the pointers are word aligned, which
	can be pretty dangerous.....

	Consider, for example short pointers to floats. If we have 7 rows
	(or any odd number), then the allocated space for the first float
	will fall on a 16-bit boundary..... 

harish pu. hi.				harish at ecelet.ncsu.edu
					harish at ecebucolix.ncsu.edu



More information about the Comp.lang.c mailing list