Dynamic Multidimensional Array or Alternatives
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
OK, I'll tell you how to find what you're looking for. :|
- Fire up a browser and visit www.google.com.
- In the search box, type "multidimensional arrays". (Omit the quotes.)
- You'll see a page of links. Click on the second link (www.eskimo.com/~scs/cclass/notes/sx4ba.html).
- Read the page content.
/ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
i'd use a vector of vectors. -c
#define O 0.05 #define I(c) putchar(c) main(){int I(int);double l[6];char lO[5];for(*(lO+1)=0;*(lO+1)<'2';I(0x0A),(*(l+5))=-25*O+((*(lO+1) )++)*O)for((*(lO+2))=0;(*(lO+2))<'2';(*(l+4))=-40.*O+((*(lO+2))++)*O){for((*(l))=(*(l+1))=0, *(lO)=1;++*(lO)&&(((*(l+2))=((*(l))*(*(l))))+((*(l+3))=((*(l+1))*(*(l+1))))<4.);(*(l+1))=(*( l))*(*(l+1))+(*(l+5))+(*(l+1))*(*(l)),(*(l))=((*(l+2))-(*(l+3)))+(*(l+4)));I((*(lO)?42:0x20));}}
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
oups Dynamic Multidimensional Array are very easy to create... but harder to use. look at the creation of a 2d Array int x = 100; int y = 200; int *MyArray; MyArray = new int[x][y]; and to acces (here initialisation) you must do: int i,j; for(i=0;i
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
What kind of data do you want to store?
Todd Smith
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
C++ doesn't directly support dynamic multidimensional arrays, however there are ways although a bit more complicated than necessary. Here is another example on how to create one. Unlike Remi Morin's "version" this one is a little harder to create but alot easier to use. And it's alot faster too I guess, since there is no multiplication necessary to access the data. This version does take up a bit more memory though...:
#include "memory.h"
#include "new.h"
//
void main()
{
int x = 4, y = 7, z = 9; // 3D array
int i, j, k;
//
// Allocate memory
int nSize = x * y * z * sizeof(int);
int* p = (int*)::operator new(x * y * z * sizeof(int));
int* q = p; // Used while creating the array
::memset(p, 0, nSize); // Initialize all array data to zero
//
// Create the array
int*** a = new int**[x];
for (i = 0; i < x; i++)
{
a[i] = new int*[y];
for (j = 0; j < y; j++)
{
a[i][j] = new (q) int[z];
q += z;
}
}
//
// Set values to all values in the array
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
for (k = 0; k < z; k++)
{
a[i][j][k] = i + (j * x) + (k * x * y);
}
}
}
//
// Deallocate the array - NB! No destruction
for (i = 0; i < x; i++)
{
delete[] a[i];
}
delete[] a;
::operator delete(p);
}Remember that the array objects will not be destructed using the destructor (if any) when deleted. It's possible to make that happen too of course by implicitly calling each and every destructor before deleting the array data. This must be done if the data stored in the array is not of a primitive type, or if for some reason the objects do not need to be destructed. The included "new.h" file is necessary for using placement new used in the last for-loop in the process of creating the array. This places the allocated object(s) (int's in this example) at the specified pointer (q). Sprudling :)
-
Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi
There are all sorts of structures, depending on what you're storing and how the "dynamic" bit occurs. Often you'll want to create a class for each dimension. Along the lines of
class Cell { .. whatever each cell stores }; class Row { .. .. .. Cell &operator[](const int idx); } class Matrix { .. .. Row &operator[](const int idx); };
Then you can do:Matrix mat(...); Cell v1,v2; mat[row][column] = v1; v2 = mat[row][col];
How the data are to be organised within the Matrix and Row classes depends on how it's used. Is it sparse? Does it grow and shrink? Are new elements added in the middle or only appended? You can use one of the standard collection classes within your classes, or you can brew your own list or array structure. You may also want to add a exception for index out of range. -
C++ doesn't directly support dynamic multidimensional arrays, however there are ways although a bit more complicated than necessary. Here is another example on how to create one. Unlike Remi Morin's "version" this one is a little harder to create but alot easier to use. And it's alot faster too I guess, since there is no multiplication necessary to access the data. This version does take up a bit more memory though...:
#include "memory.h"
#include "new.h"
//
void main()
{
int x = 4, y = 7, z = 9; // 3D array
int i, j, k;
//
// Allocate memory
int nSize = x * y * z * sizeof(int);
int* p = (int*)::operator new(x * y * z * sizeof(int));
int* q = p; // Used while creating the array
::memset(p, 0, nSize); // Initialize all array data to zero
//
// Create the array
int*** a = new int**[x];
for (i = 0; i < x; i++)
{
a[i] = new int*[y];
for (j = 0; j < y; j++)
{
a[i][j] = new (q) int[z];
q += z;
}
}
//
// Set values to all values in the array
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
for (k = 0; k < z; k++)
{
a[i][j][k] = i + (j * x) + (k * x * y);
}
}
}
//
// Deallocate the array - NB! No destruction
for (i = 0; i < x; i++)
{
delete[] a[i];
}
delete[] a;
::operator delete(p);
}Remember that the array objects will not be destructed using the destructor (if any) when deleted. It's possible to make that happen too of course by implicitly calling each and every destructor before deleting the array data. This must be done if the data stored in the array is not of a primitive type, or if for some reason the objects do not need to be destructed. The included "new.h" file is necessary for using placement new used in the last for-loop in the process of creating the array. This places the allocated object(s) (int's in this example) at the specified pointer (q). Sprudling :)
I've try it but I have a problem with this line in debug mode int* p = (int*)::operator new(x * y * z * sizeof(int)); I'm unable to compile. this error raised E:\woop\woopDoc.cpp(827) : error C2665: 'new' : none of the 4 overloads can convert parameter 1 from type 'char [20]' In release mode it's ok it work very fine Why it dos'nt work in Debug? Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
-
I've try it but I have a problem with this line in debug mode int* p = (int*)::operator new(x * y * z * sizeof(int)); I'm unable to compile. this error raised E:\woop\woopDoc.cpp(827) : error C2665: 'new' : none of the 4 overloads can convert parameter 1 from type 'char [20]' In release mode it's ok it work very fine Why it dos'nt work in Debug? Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com