Multi-dimentional arrays


I have been looking into ways of doing multi-dimentional arrays without
suffering performance loss from using many pointers etc.
atm, i have set it up with a static array. fixed at the maximum size
possible. While this is only 100mb, it would be nice to allocate the
space with malloc etc.

I am using C++ combined with some C code for ease of use. My data is 3D

ulong voxel_data[VOX_SIZE_X][VOX_SIZE_Y][VOX_SIZE_Z];

they are the maximum

I tried many different ways, but the only one that worked was using a **
and malloc each extra dimension. But I think that was seriously slow and
took up more memory because extra pointers were used.

if((voxel_data[VOX_SIZE_X][VOX_SIZE_Y][VOX_SIZE_Z] =(ulong)
printf(“not enough ram\n”);
return FALSE;

that is some code that does not work, that I tried when i had

ulong *voxel_data[VOX_SIZE_X][VOX_SIZE_Y][VOX_SIZE_Z];

I looked on the internet and I found an example, (at the

also has a good
examplke at the botom as well.

If any one has any sugestions/critisism, please do comment.



Hypermedia Research Centre
Sanyo R&D Headquarters, Tokyo
Tel: +81 (0)3 5803 3566
Fax: +81 (0)3 5803 3640
Email: jg at
Please use open standard file formats for attachments

First: This thread is off-topic and some people will be angry with you
for posting (and with me for answering) - so please post questions not
related to SDL to another list. However, try using …

malloc(VOX_SIZE_XVOX_SIZE_YVOX_SIZE_Z * sizeof(ulong) )+++++++++++++++

… instead of your current code. You allocated an x * y * z array of
byte-sized entries, but you need to allocate ulong-sized entries.

Regarding your actual question: The snip below is from the malloc man
entry (on a Solaris, but it should be like any *ix):

 If malloc(), calloc(), or realloc() returns  unsuccessfully,
 errno will be set to indicate the error. The free() function
 does not set errno.

The malloc(), calloc(), and realloc() functions will fail

 ENOMEM    The physical limits of the system are exceeded  by
           size  bytes of memory which cannot be allocated.

 EAGAIN    There is not enough memory available at this point
           in time to allocate size  bytes of memory; but the
           application could try again later.

To summarize: If your malloc fails, check errno to see if it is equal to
ENOMEM - in that case you need to rethink your strategy.

I hope this answered your question, but please remember to limit
SDL-list questions to SDL.

Have a nice day.


NOTE: This email is PRIVATE and does not reflect the views of my