// Allocate3DArray by David Maisonave (609-345-1007) (www.axter.com) #include #include void ***Allocate3DArray(int TypeSize, int x, int y, int z) { void *** pppx = malloc(x*sizeof(void**)); void ** pool_y = malloc(x*y*sizeof(void*)); void * pool_z = malloc(x*y*z*TypeSize); unsigned char *curPtr = pool_z; int i_x, i_y; if (!pppx || !pool_y || !pool_z) return NULL; for(i_x = 0; i_x < x; ++i_x){ pppx[i_x] = pool_y; pool_y +=y; for(i_y = 0;i_y < y;++i_y){ pppx[i_x][i_y] = curPtr; curPtr +=z; } } return pppx; } void Free3DArray(void*** Array) { free(**Array); free(*Array); free(Array); } /* The above code is portable when used with char type However, according to C standards section 6.2.5-26, this code is not portable when used with other types. Even though this code is not portable with other types, the code will still work on most implementations. If portability is needed, then specialize the above code to the desired type. That will make it portable. */