🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Dynamic memory allocation for arrays

Started by
6 comments, last by LackOfKnack 24 years, 5 months ago
Hey! I''m not very good at C++ yet, so I have a newbie question: How do you dynamically allocate memory for a two-dimensional array? I''m using BTC++ 3 for DOS for now. I had something like: int *table[][]; ... malloc whatever; It doesn''t seem to work very well. Any suggestions? Thanks.
Lack
Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
Advertisement
I think it goes something like this:

int **table;

table = new int*[rows];

for (int r = 0; r < rows; r++)
table[r] = new int[cols];


Edited by - Bugdude on 1/15/00 6:28:34 AM

Edited by - Bugdude on 1/15/00 6:29:03 AM
Thank you very much, it works great. Hopefully sometime I''ll learn why...


Lack
Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
Direct quote of example from VC++ documentation on C++

float *fp;
fp = new float[10][25][10];
The reason that it works is that C++ uses double indirection on it''s 2d arrays.

Say you have float a[10][10] and want to access a[0][0].
First it looks at a and says oh! a float **, the next set of brackets must mean that I''m indexing it for a float *. So when it sees a[0] it translates it into:
(float *)(*(a + 0 * sizeof(float *)))
Then it says, oh! I have a float *, the next set of brackets must mean I''m indexing it for a float. So a[0][0] gets translated into:
(float)(*((float *)(*(a + 0 * sizeof(float *))) + 0 * sizeof(float)))

So if you just allocate 10 * 10 floats, you won''t allocate that extra layer of (float *). Leading it to barfing on the indirections.
Now I get it. Thanks, SiCrane.

But _dot_, your code didn''t seem to work. BTC++ gives me some errors. Maybe it''s the MS C++ deviation only?

And Bugdude, the code worked initially, but whenever I access a co-ordinate:

table[3][4] = 9;

...it changes the whole table[?][4] row to 9. Is it a bug in my code? Or something with the Borland compiler? I couldn''t seem to find it.

Thanks for all your help.


Lack
Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
Are you sure you''re not initializing your (int *)''s to the same array? The code works fine on my copy of Turbo C++.
Personally I find this manner of declaring arrays unintuitive and bug prone, but here''s the *full* passage that _dot_ quoted:

quote:
When new is used to allocate a single object, it yields a pointer to that object; the resultant type is new-type-name * or type-name *. When new is used to allocate a singly dimensioned array of objects, it yields a pointer to the first element of the array, and the resultant type is new-type-name * or type-name *. When new is used to allocate a multidimensional array of objects, it yields a pointer to the first element of the array, and the resultant type preserves the size of all but the leftmost array dimension. For example:

new float[10][25][10]

yields type float (*)[25][10]. Therefore, the following code will not work because it attempts to assign a pointer to an array of float with the dimensions [25][10] to a pointer to type float:

float *fp;
fp = new float[10][25][10];

The correct expression is:

float (*cp)[25][10];
cp = new float[10][25][10];

The definition of cp allocates a pointer to an array of type float with dimensions [25][10] — it does not allocate an array of pointers.


Despite anything else I''d like to say, I''ll let the quote speak for itself.

This topic is closed to new replies.

Advertisement