Dynamic Arrays?
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]?
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
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
int *array;
array = new int[value];
and then when your done with it:
delete [] array;
For a single dimensional.
-Medgur
array = new int[value];
and then when your done with it:
delete [] array;
For a single dimensional.
-Medgur
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
int * parray;
parray = new int[x][y];
delete [] parray;
doesn''t work
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
myArray[x][y] = 12;
You do it like this:
myArray[x + y] = 12;
Edited by - Muzzafarath on July 14, 2000 4:10:52 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
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
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
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;<br>delete[] array;<br><br>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.<br><br> </i>
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;<br>delete[] array;<br><br>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.<br><br> </i>
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]..
(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]..
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];
..-=gLaDiAtOr=-..
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=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement