Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Medgur

Multi dimensioned arrays at runtime

This topic is 6940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a quick question, I know how to produce multi-dimensioned arrays at runtime in C, but my experience in C++ is limited, so defining them at runtime is somewhat of an enigma to me with C++. So, if I had an dual-dimensioned integer array maptile[mapwidth][mapheight] how would I define it at runtime with the new command? Thanks in advance, -Medgur

Share this post


Link to post
Share on other sites
Advertisement
I''m not answering your question directly, but... I''d strongly suggest you just use nested STL vectors. But if you want to use new, check out:

http://www.cerfnet.com/~mpcline/c++-faq-lite/freestore-mgmt.html#[16.15]

(Also read the subsequent question.)

-Brian

Share this post


Link to post
Share on other sites
Try this:

int array = new int[width*height];
array[x+y*width] = value;

From what I've read in previous posts, it is not possible to actually create a multidimentional array with the new command. I've heard you can create an array of pointers to arrays but that looks messy:

int array = new *int[height];
for( int x = 0; x < height; x++ )
array[x] = new int[width];

Or something to that manner.

Hope that helps.

OneEyeLessThanNone
- My +3 headphones are now utterly useless.

please note that i didnt use i in the for loop as i in square brackets [] will make the text itallic like this text right here, i also did not use a as that gives you a link

please kill me, i have nothing better to do than to edit my posts to perfection and then not like them anymore


Edited by - OneEyeLessThanNone on 4/23/00 8:21:22 PM

Share this post


Link to post
Share on other sites
Whats wrong with the C way? I have been wondering whether to use dynamically allocated arrays, but I''ve come across a problem. Will there be enough memory on the heap to hold lots of classes for something like a sprite? How can someone know how much memory is left on the heap? Is it worth implementing something like this for a sprite manager?

Share this post


Link to post
Share on other sites
To one-eye-less:

Try putting source code inbetween code tags, it really works.
I mean these brackets:
( I hope you can see the closing tags after editing this )
[code]  

Okay so you can't see the closing tag, but you can imagine it.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**


Edited by - MadKeithV on 4/24/00 3:22:30 AM

Share this post


Link to post
Share on other sites
Thanks for the replies. I thought it would be something like C++ didn''t support multi-dimensional runtime arrays with the new command. Ah well, I''ve got that alternate working now.

BTW-the problem with c is that emulating C++ code can be messy, and why bother? Any speed increase will be marginal anyways (if the process is rarely used) and most of the time, probably not worth the hassle.

Thanks,
-Medgur

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
int **Array;

Array = new (int *)[5];
for (int x = 0; x != 5; x++)
Array[x] = new int;

This is the right way to dynamically allocate a 5x5 array of ints in C++.

Share this post


Link to post
Share on other sites
Anonymous... that is not the right way... if you do it that way, to access an array element you have to dereference a pounter twice. And it wastes a little bit of space... IMHO the best way would be to do something like this

int *array = new int[xsize*ysize];

to access an element in this array
array[x+y*xsize]

Torval

Share this post


Link to post
Share on other sites
I like anonymous''s way better. What''s wrong with dereferencing a pointer twice? Is it any worse than doing addition and multiplication whenever you want an element? I think it''s more intuitive, when wanting the 3rd element in the 5th column to say array[5][3] than to say array[3+5*xsize]. But whatever you prefer.

aig

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!