dynamic multidimensional arrays in c...

Started by
22 comments, last by earl3982 21 years, 8 months ago
quote:Original post by davepermen
i wanted to provide a save way, not a fast way. to speed this up is very simple, and, well, if c would know inlines it would get as fast as directly bar written (as i use to do this anyways)

C has supported inline functions since 1999, but not all compilers support inline functions for C.

Advertisement
looks like i am slightly outdated on the c standard, heh.

trzy: in c++, you can use static member functions and remove implicit passing for functions you dont need the struct to do the work you need.

a compiler cant optimize parms away since at anytime you may use them or the memory that they reserve.
Well... off the top of my head, I would do something like this:

      typedef char byte;#include <stdlib.h> #include <memory.h> byte** allocArray(int xDim, int yDim)	{	int i;		byte** array2D;	byte* buf;		buf = malloc(xDim * yDim + (xDim*sizeof(byte*)));	array2D = (byte**)buf;	for(i=0; i<xDim; i++)		array2D[i] = &buf[i*yDim + (xDim*sizeof(byte*))];	#ifndef NDEBUG	memset(&array2D[0][0], 0xcd, xDim*yDim);#endif	return array2D;	}void freeArray(byte*** array)	{	free((void*)(*array));#ifndef NDEBUG	*array = 0;#endif	}	int main()	{	int i, j;	byte** myarray = allocArray(10, 12);	for(i=0; i<10; i++)		for(j=0; j<12; j++)			myarray[i][j]= 1;	freeArray(&myarray);	return 0;	}      



edit: include bug...

[edited by - Magmai Kai Holmlor on July 30, 2002 12:28:52 AM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
thx for everyone''s help.

This topic is closed to new replies.

Advertisement