Advertisement

Dynamic Arrays?

Started by July 14, 2000 01:40 PM
16 comments, last by vbisme 24 years, 5 months ago
I''m pretty sure what Muzza was talking about would be:
int max_x, max_y, x, y;// do stuff to figure out what above values should beint *array = malloc( sizeof(int) * max_x * max_y );// then to access array member array[x][y]array[(x * max_y) + y] = whatever; 


Frankly Galdiator''s way is much neater and much more typical, but the above is the one of the old C ways of doing things.

random-nomad
Yes, Gladiator is right, a typo I''m afraid...
Advertisement
the method ((y * ymax) + x) would work great if the xmax and ymax are the same like so with a 3x3...

[0][1][2] (0,0)(1,0)(2,0) x >>>>>>
[3][4][5] (0,1)(1,1)(2,1) y down
[6][7][8] (0,2)(1,2)(2,2)

but if the xmax and ymax are different it would have jumps as in 3x5...

[0][1][2] (0,0)(1,0)(2,0)
[5][6][7] (0,1)(1,1)(2,1)
[10[11][12] (0,2)(1,2)(2,2)
[15][16][17] (0,3)(1,3)(2,3)
[20][21][22] (0,4)(1,4)(2,4)

See what I mean?

the pointer to pointer idea seem to be agreeable, but can someone explain?
Why not just use STL vectors ?
quote:
Why not just use STL vectors ?


Exactly, and you can still use [][], e.g.

            std::vector< std::vector<int> > vec2d(10);//resize all sub-vectors to 10for(int i = 0; i< 10; ++i){	vec2d<i>.resize(10);}//use an element in the vectorvec2d[2][5] = 7;        


Edited by - Wilka on July 15, 2000 1:54:52 PM
Vectors? hmm...any resources on how to use them? As for...

#include
int main()
{ int **myArray;
int x, y;
x = 10;
y = 15;
myArray = new int*[x];
for (int i = 0; i < x; i++)
myArray = new int[y];
myArray[0][0] = 12;
printf("%d", myArray[0][0]);
for (i = 0; i < x; i++)
delete [] myArray;<br> delete [] myArray;<br> return 0;<br>}<br><br>on line 9, how come you don''t have to "deference" the myArray pointer with the "*" operation to get the value instead the address of?<br><br> </i>
Advertisement
I wanted to avoid creating a temporary vector when make a 10 by 10 array, but since your probably not going to be creating many multi-dimensional arrays the speed & memory difference wont matter. So you might as well use the nicer construct-from-temporary method:

    std::vector< std::vector<int> > vec2d(10, vector<int>(10));//access an element in the vectorvec2d[2][5] = 7;    


If it does end up being slow, you can always speed it up latter.
vbisme:
the reason you don't have to deference the vector is the elements are stored by value and the index operator returns a reference. e.g.

        std::vector<int> v1(10); // a vector of 10 int'sv1[0] = 1; // this will return int&, which gets assigned to 1;std::vector<int*> v2(10); // a vector of 10 int pointersv2[0] = new int; // this returns a int*& which gets assigned to a new int.*v2[0] = 1; // this deferences the pointer in element 0 and assigns it to 1        


For more info on std::vector have a look at http://www.sgi.com/Technology/STL/Vector.html




Edited by - Wilka on July 15, 2000 1:56:37 PM

This topic is closed to new replies.

Advertisement