Archived

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

Dynamic Arrays?

This topic is 6358 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

Can you create dynamic arrays in C++? Something like... int myArray[][]; and then sign it''s size later? int x, y; x = 10; y = 15; and somehow set myArray to be myArray[x][y]?

Share this post


Link to post
Share on other sites
the method of creating the pointer in the free store works, but it doesn''t work for multimension arrays.....

int * parray;
parray = new int[x][y];
delete [] parray;

doesn''t work

Share this post


Link to post
Share on other sites
Ack! Muzzafarath, if you use:
myArray[x + y] = 12;

This means that (6,0) maps to the same as (0,6)!

Here''s how you do it (assuming y_dim equals the dimensions of the array in the y directions):

myArray[(y * y_dim) + x] = 12;

Now, if y_dim is a power of 2, you can instead do bit shifts for even faster access. Ex:

Say y_dim is 16 (2 to the 4th). We can then access an element of myArray as follows:

myArray[(y << 4) + x]

All right, enough optimization. It''ll work now.

Vyvyan

Share this post


Link to post
Share on other sites
If you want to be able to access it using array[x][y] then you have to alloc it like this (or in a símilar way using malloc):

int **array;

int width = 10; // what ever....
int height = 20; // --||--

array = new int[x];
for (int i = 0; i < x; ++i)
array = new int[y];

// Use the array here

for (int i = 0; i < x; ++i)
delete[] array[i];
delete[] array;

But personally I would preferr the malloc/free method, if you dont like accessing the array using array[x + y*width] (I assume this is what Muzzafarath means) write a macro.

Share this post


Link to post
Share on other sites
Interesting Muzza....but... check out the coordinate problem

(0,0)(1,0)(2,0)
(0,1)(1,1)(2,1)
(0,2)(1,2)(2,2)

and the array for that is...array[0][0], array[1][0]...array[1][2], array[2][2].....ok if one dimension...

we have a probem because there are more than on coordinate that the x and y add to the same number such as (1,0) and (0,1). So if you say...

array[x + y] to be array[1 + 0] that could also refer to coordinate (0,1) as in array[0 + 1]..

Share this post


Link to post
Share on other sites
Here is an example...

Jesse, your code wouldn''t work

This is what you have...

array = new int[x];

but that needs to be,,

array = new int*[x];


    
#include <stdio.h>

int main()
{
int **myArray;
int x, y;

x = 10;
y = 15;

myArray = new int*[x];

for (int i = 0; i < x; i++)
myArray<i> = new int[y];

myArray[0][0] = 12;

printf("%d", myArray[0][0]);

for (i = 0; i < x; i++)
delete [] myArray[i];
delete [] myArray;

return 0;
}



..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
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 be

int *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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 10

for(int i = 0; i< 10; ++i)
{
vec2d<i>.resize(10);
}

//use an element in the vector

vec2d[2][5] = 7;


Edited by - Wilka on July 15, 2000 1:54:52 PM

Share this post


Link to post
Share on other sites
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[i];
delete [] myArray;
return 0;
}

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?

Share this post


Link to post
Share on other sites
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 vector

vec2d[2][5] = 7;


If it does end up being slow, you can always speed it up latter.

Share this post


Link to post
Share on other sites
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's


v1[0] = 1; // this will return int&, which gets assigned to 1;

std::vector<int*> v2(10); // a vector of 10 int pointers


v2[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

Share this post


Link to post
Share on other sites