is that in the latter case you can change the size of the allocated memory using realloc().

However, both are equivalent in terms of accessing the allocated memory. You can access arr[x][y] in both the cases.

06-13-2002

nano_nasa

Thanks for the suggestions everyone. I had tried declaring the variables as static before and was still running out of memory. Once I found the target settings in the compiler I was able to change the amount of memory allocated to the stack and heap. For now I'm going to stick with the declarations

Code:

short retr[256][512],appr[256][512];

as I seem to be unable to use the [x][y] scripting when using the malloc() on my platform. My compiler doesn't like me calling to the 2D array when it was declared

I'm sorry, I was wrong. On none of the platforms and compilers that I use, can a double subscript be applied to a malloc()ed pointer.

However, a single subscript can. That's why I wrote so cocksuredly - "both are equivalent in terms of accessing the allocated memory" :D

Thanks for the enlightenment.

Coming to your problem, I think filling up stack with large variables is not all that good an idea.

A messy solution is: use malloc() to allocate memory in heap, define accessor/mutator functions to manipulate this memory. Something like:

Code:

modify_arr( x, y, value );
val = fetch_arr( x, y );

06-13-2002

nano_nasa

Re: mea culpa

Quote:

Originally posted by Ruchikar
Coming to your problem, I think filling up stack with large variables is not all that good an idea.

I am worried about the same thing. Eventually I'm going to need 16 of these 2D arrays and my stack would be way too overloaded. Setting these declarations into the global moves everything to the heap, correct? I just need to make sure I carve out enough memory to run everything. Thanks you've all been a lot of help.

nn3a

06-13-2002

Salem

If you really want to allocate a 2D array, using malloc, and keep the ability to use [x][y] indexing, then do this

short retr[256][512];

Would be
short (*retr)[512];
retr = malloc ( 256 * sizeof(*retr) );

Yes, all the () are necessary

This only works if the minor dimension (512) is a compile time constant. If you want something which varies at runtime - ask :)