[comp.lang.c] 2d arrays in C

ok@mudla.cs.mu.OZ.AU (Richard O'Keefe) (05/28/90)

We've had a lot of fun arguing about what dynamic arrays in C _should_
look like and whether it's doable.  To be really helpful and productive,
I thought I'd share a README file with you.  (One fine point:  the data
structure used here for 2d arrays would **NOT** work with fwrite.)
This came out in 1988, I think to comp.sources.misc.  I believe that the
author of the package has since moved.

	/* README for xxalloc */

xxalloc is a family of routines for dynamic array manipulation in one,
two and three dimensions.  Routines are included for allocation,
initialization, printing, renumbering, and freeing both arrays of
structures and arrays of simple types.  Since the "edge-vector" approach
is used for two and three dimensional arrays, this set of routines
allows for the development of reusable subroutine libraries without
regard to some "maximum" dimension.  Both positive and negative indices
are allowed. 

When I converted to C several years ago after my first large program in
FORTRAN, the first thing I wanted to do was to have adjustable
multi-dimensional arrays.  This can be achieved in C (sort of, the array
dimensions are fixed) through the use of pointers (the so called
"edge-vector" approach, see also K&R p.  110).  I then added routines
for manipulating structures and routines for renumbering the indices,
initializing, and printing.  These routines also solve the problem of
not being able to have 2D arrays greater than 64k on machines with 16
bit addresses (ie.  PC's). 

Installation instructions are in the makefile.  A test program is
included to exercise most of the package.  I have compiled this package
on many machines (SYS5, BSD, MSDOS) without a hitch.  The man page uses
some abbreviation to save space since there are actually 109 functions
in this package. 

--
Harold G. Walters                 Internet: walters@ce.okstate.edu
School of Civil Engineering       Uucp: {cbosgd, ihnp4, uiucdcs}
Oklahoma State University               	!okstate!osubem!walters
Stillwater, OK 74078 "If all you have is a hammer, everything looks like a nail"

brnstnd@stealth.acf.nyu.edu (05/30/90)

In article <4273@munnari.oz.au> ok@mudla.cs.mu.OZ.AU (Richard O'Keefe) writes:
> We've had a lot of fun arguing about what dynamic arrays in C _should_
> look like and whether it's doable.  To be really helpful and productive,
> I thought I'd share a README file with you.  (One fine point:  the data
> structure used here for 2d arrays would **NOT** work with fwrite.)

One fine point: The reason its 2d arrays wouldn't work with fwrite is
that it implemented them as pointers to pointers to data. In contrast,
when 2d arrays are implemented as huge square chunks addressed by a
single pointer, they work *perfectly* with fwrite. The same applies to
3d arrays implemented as huge cubic chunks, etc.

(Of course, one method may be more or less efficient on different
machines: multiple pointers use more memory accesses, while true
multidimensional arrays use more CPU calculation, plus even more memory
if registers aren't free for variable dimensions. I'd use multiple
pointers on a microcomputer, true arrays on a real computer.)

---Dan