Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!dsi!dave From: dave@dsi.COM (Dave Rifkind) Newsgroups: comp.lang.c Subject: Re: Style for handling matrices in C Message-ID: <278@dsi.COM> Date: 16 Oct 89 07:30:01 GMT References: <7229@cognos.UUCP> Reply-To: dave@dsi.UUCP (Dave Rifkind) Organization: KFW Corporation, Newbury Park, CA Lines: 24 In article <7229@cognos.UUCP> alanm@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.