#### Archived

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

# Dynamic Arrays?

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

## 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]?

hmmm... hell no.

##### Share on other sites
int *myArray;

int x, y;
x = 10;
y = 15;

myArray = malloc((x * y) * sizeof(int));

// Do whatever you want to myArray...

free(myArray);

Don't quote me on that code

Edited by - Muzzafarath on July 14, 2000 3:00:12 PM

##### Share on other sites
int *array;
array = new int[value];

and then when your done with it:
delete [] array;

For a single dimensional.

-Medgur

##### 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 on other sites
If you use "my" version, you just access it a little bit different. Instead of doing it like this:

myArray[x][y] = 12;

You do it like this:

myArray[x + y] = 12;

Edited by - Muzzafarath on July 14, 2000 4:10:52 PM

##### 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 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 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 on other sites
Here is an example...

This is what you have...

array = new int[x];

but that needs to be,,

array = new int*[x];

  #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;}

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013567
×