🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2226

Re: dynamic creation of multi-dimentional arrays

#2226
From: Jos A. Horsmeier
Date: Sun, 10 Mar 1996 16:32
107 lines
3849 bytes
In article <4hlaf4$4eu@solutions.solon.com>, don@mars.dgrc.doc.ca wrote:

|I might word is like, "Are they any defacto-standard data structures and
|functions available to allocate/manipulate N dimensional arrays, with 
|dimension sizes  N0, N1, N2, ... NN when N, N), N1, N2, ... NN are completely 
|unknown at compile time?"

|I have a user who basically wants to do something like the following.
|
|        int ndim, *dimsizes, i;
|
|        printf("How many dimensions will the array have? ");
|        sscanf("%d", &ndim);
|        dimsizes = malloc(ndim * sizeof(int));
|
|        for(i = 0; i < ndim; ++i)
|        {
|                printf("enter the width of dimension %d: ", i);
|                sscanf("%d", &dimsize[i]);
|        }
|
|Then the user wants to allocate the required storage, and declare functions
|to insert values into the "array".  I declared a pointer and malloc'ed
|sizeof(type) * N) ... NN, but accessing this thing became beastly.
|
|The problem is that not knowing the number of array indexes, it gets 
|difficult to index into the data.
|
|Is there any "common" way of doing this?

I don't know of any 'common' way of doing this, but recursion comes to
mind here and recursion sometimes allows you to solve things in a quite 
'natural' way. Here's how I would do this (if I ever had to do this ;-)

Given an array of dimension sizes and the number of dimensions, say, 'n'
and the size of a single element 'size', the following routine allocates
storage for the entire beast:

void* md_malloc(int* dims, int n, int size) {

	int	i;
	void**	dv;

	if (n == 1)
		return malloc(dims[0]*size);
	else {
		dv= malloc(dims[0]*sizeof(void*));
		for (i= dims[0]; i--; )
			dv[i]= md_malloc(dims+1, n-1);
		return dv;
	}
}

If there's just one dimension given, md_malloc() simply calls malloc
to allocate the array for you; otherwise it allocates a dope vector
that stores all the slices of the sub-matrices and it calls itself
recursively to allocate those sub-matrices.

Here's a way to free such a thing again:

void md_free(void* mem, int* dims, int n) {

	int	i;
	void**	dv= mem;

	if (n == 1)
		free(mem);
	else {
		for (i= dims[0]; i--; )
			md_free(dv[i], dims+1, n-1);
		free(dv);
	}
}

Note that md_free() uses the same idea as md_malloc(), i.e. a one dimensional
thing is freed as usual, otherwise it'll call itself recursively to free
the sub-matrices before it frees the dope vector itself.

And finally, given an array of n indices for the matrix, here's a little
thingy that returns a pointer to the corresponding element:

void*	md_val(void* mem, int* dims, int n, int size) {

	void**	dv= mem;
	char*	e = mem;

	if (n == 1)
		return e+dims[0]*size;
	else
		return md_val(dv[dims[0]], dims+1, n-1, size);
}

Of course you can use the '[]' notation to access the slices of the
sub-matrices yourself, although it's not of much use because the number
of dimensions (and their sizes) are not available at compile time.

Please note that the functions above don't check for any errors or
exceptions; if malloc() fails or if the caller of those functions cheats,
a total chaos and a likely segmentation fault are yours ... ;-)

I hope this helps you out a bit,

kind regards,

Jos aka jos@and.nl
-- 
Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
Technical Details
Message-ID: <4hv07h$jd1@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4hlaf4$4eu@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: jos@and.nl (Jos A. Horsmeier)
Newsgroups: comp.lang.c.moderated
Subject: Re: dynamic creation of multi-dimentional arrays
Date: 10 Mar 1996 10:32:49 -0600
Organization: AND Operations Research B.V.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hv07h$jd1@solutions.solon.com>
References: <4hlaf4$4eu@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: WinVN 0.99.5