Multi dimensioned arrays at runtime
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
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
http://www.cerfnet.com/~mpcline/c++-faq-lite/freestore-mgmt.html#[16.15]
(Also read the subsequent question.)
-Brian
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
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
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?
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
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 )
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 )
</pre> <br>Okay so you can't see the closing tag, but you can imagine it.<br> <br><br>#pragma DWIM // Do What I Mean!<br><b>~ Mad Keith ~ </b> <br>**I use Software Mode**<br><br><br>Edited by - MadKeithV on 4/24/00 3:22:30 AM
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
C++ is an extension to C so you can make almost everything in C++ with C commands.
Visit our homepage: www.rarebyte.de.st
GA
Visit our homepage: www.rarebyte.de.st
GA
Visit our homepage: www.rarebyte.de.stGA
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
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
April 24, 2000 08:13 PM
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++.
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++.
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
int *array = new int[xsize*ysize];
to access an element in this array
array[x+y*xsize]
Torval
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
aig
aig
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement