Skip to main content
gamedev.net
🔒 Locked

VC++: resize a multi-dimension array

Started by penetrator Jan 14, 2003 at 6:18 PM 7 replies 350+ views
Shannon Barber
Shannon Barber
Short answer: You can''t.

The reality is, you have many choices:

Hackish answer:

Int*** threeD = new int**[40];
for(int j=0; j<40; ++j)
{
threeD[j] = new int*[4];
for(int k=0; k<2; k++)
threeD[j][k] = new int[2];
}


Gentlemen''s Agreeement: Int* threeD = new int[40*4*2];

Ugly answer: Nest a bunch of std::vector''s

Long answer: Write a multi-dimension array class.

Shorter answer: Use the one that''s part of Boost
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
MajorShredd
MajorShredd
The most brute force method is to create a new array the size you want, copy what''s within the bounds of the old one while initializing anything else, copy the pointer from the old array to the new array, and delete the old one. It''s pretty brutal, but it works.
penetrator
penetrator
Magmai, using your example, how would i fill the multiarray ?

i tried smt like:

threeD[39][1][1]=1234; // this work
threeD[39][2][1]=1234; // this dont, but 2nd dimensions shouldn''t be up to 4 elements ?



www.web-discovery.net

Qw3r7yU10p!
Qw3r7yU10p!
What would the relation be between the layout of the data in one array and the layout in the other?

How would you decide what value goes where? Do you have a transformation algorithm?

I''d create a new array and use the algorithm to put the old values into the new array. You can''t just resize it and expect the data to be in the right place.

If you don''t want to keep the data then just start with a new array of the correct dimensions.
owl
owl
why dont you use a single array of struct?

pseudo like:


struct etc
value1 as int
value2 as int
/struct



struct_arr[] as etc



struct arr = new etc[40]



function resize_preserve(new_size, old_arr) //returns pointer
{
tmp_newArr = new etc[new_size];
memcpy(newArr,old_arr, nbytes);
return newArr;
}


[edited by - xaxa on January 15, 2003 7:29:41 AM]
[size="2"]I like the Walrus best.
penetrator
penetrator
This trouble i have starts because i get the array dimension reading a .ini file. My c++ knowledge is poor, but i understood i must declare dimensions as const values, therefore if i need to create an array at run-time, i.e. reading values from a .ini file, i must resize it. It''s easy to do with 1 dimension array, but i must create a function like this:

int dim1=40;
int dim2=20;
int dim3=3;
// now i should create a 3 dimensional array
easy with VB, un-understandable (for me) with VC++

i tried Magmai code, but if i fill the 2nd array dimension i get an error:

array[40][1][1]=1234; // ok
array[40][2][1]=1234; // error, but shouldn''t accept values from [1][1][1] up to [40][20][1] ?





www.web-discovery.net

owl
owl
resizing arrays dinamically is slow for games, you should try to avoid doing it. The anyway the pseudo code I posted above should work (slow) but fine I guess. I can''t help you with c++ because I dont have the time sorry. good luck
[size="2"]I like the Walrus best.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
array[40][2][1]=1234; // error, but shouldn't accept values from [1][1][1] up to [40][20][1] ?


When you say error, what do you mean? At compile time or runtime? It shouldn't cause an error to the compiler (even though it's wrong).

Anyway, whatever, you're getting very confused.

In c and c++ arrays are counted from zero upwards so to index an array of size 5 you use indices 0,1,2,3 and 4. If you use 5 then you're accessing some area of memory outside of the array. I believe Visual Basic counts from 1. Counting from zero has been done in computing since the beginning of time.

So:

int array[40][20][10];

array[0][0][0] = 1; // first element
array[39][19][9] = 8000; // 8000th element

You're overrunning boundaries and that'll probably cause a runtime error.

This example is declared at compile time as the numbers 40,20 and 10 are hard coded. If you want something more dynamic then do any of what magmai suggests.

edit:changed a "should" to a "shouldn't"

[edited by - petewood on January 15, 2003 8:53:25 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.