Hello, I was wandering if it was possible to create 2 dimensional array dynamicly. If it is possible, how can I do it.
Printable View
Hello, I was wandering if it was possible to create 2 dimensional array dynamicly. If it is possible, how can I do it.
Here is an example:
VB Code:
char me[4][4];
Creates a 2 dimensional array with 4 rows and 4 columns.
AFAIK it's impossible, with known dimensions you can though use a 1d array like this:
type* array=new type[firstdimension*second dimension];
array[firstelement*seconddimension+secondelement]
or
array[firstelement+firstdimension+secondelement]
Of course it's not impossible, kedaman =). Simple keep a pointer to std::vector, where the size of the vector is the number of rows. Then, just resize each vector to be the number of columns you want, and write a few simple overloaded [] operators. Should be a fairly easy one to make. Then, to resize rows, use realloc() (it will do any copying necessary for you), and for columns, just resize the vectors.
Z.
Come on Zaei! That's not the standard C++ array :p
However I don't understand your reasoning, it sounds more like you are making an array of arrays.
That's all a 2D array is. An Array of an Array. So, use a vector of vectors to make a dynamic 2d array =).
Z.
heyhey, thats not true, a 2d array has to be stored concecutively in memory just like regular arrays.
If it works like int x[10][10], and it's dynamic, it's a 2d array in my book. Thats what container classes are for, abstracting the interface away from the implementation, so that the user doesnt have to know whats going on under the covers =).
Z.
It doesn't work like a 2d array, that's the point with distinction between datastructures. I thought you were for lowlevel stuff Zaei, now you show the same ignorance of a Java programmer :p
I love low level as much as the next guy, but when you need a dynamic array, and dont know the numbers at run time, what are ya gonna do =)?
Z.
You do what I did
You said it was impossible =P.
And, even without vectors, here is a solution:
That should resize the array correctly, and still keep the consecutive memory requirement (kedaman =).Code:void resize(int* mat, int nx, int ny, int ox, int oy)
{
int* nmat = new int[nx*ny];
int trans;
for(int i = 0; i < nx; ++i) {
for(int j = 0; j < ny; ++j) {
if((i >= ox) || (j >= oy))
trans = 0;
else
trans = mat[j*oy+i];
nmat[j*ny+i] = trans;
}
}
realloc(mat, nx * ny * sizeof(int));
memcpy(mat, nmat, nx * ny * sizeof(int));
//delete [] nmat;
}
Z.
[edit]
Found some errors in the code, fixed.
You can't mix realloc and new... :)
why?
Z.
AFAIK It is still impossible. Simply because dimensions are static, provided one of the dimensions you can use a 1d array to act like a 2d array :)
I just demonstrated that it is possible, kedaman =P. Check the code =).
Z.
Ragged arrays are very possible. Just have an array of pointers, and allocate that. Then allocate the individual arrays off those elements.
You can't mix realloc and new because they use different allocation methods (new[] stores extra data about the size of the array, and new might do something else), and might not even use the same allocation heap!
The lack of a "renew" in C++ is something that Stroustrup mentioned and may make it into the new C++ standard :)
No Zaei you just demonstrated my workaround :) It is still a 1d array.
Are we talking technically or conceptually? :D
In an environment where everything is stored sequentially, technically, of course you cant have a 2d array, period =). Conceptually, you use the workaround, or you stick with normal C++ procedure, and create a container class (or create a regular 2d array with dimensions of the largest needed size).
Z.
it is possible to dynamically allocate 2 dimensional arrayx if one dimension is known. We talked about that kedaman:
Code:int (*ar)[5] = new int[dynarg][5];
ar[3][4] = 234;
Yep :) btw did you make a fully resizable 2d array class?
No. I have many other things to do and don<#t need it. If I ever make one I'll post it.
Ok, I don't have a need of it either, if i happen to make one i'll post it here, unless someone else has posted it already :p
Why not just a simple matrix class?
I'm sure the resizable bit's been done before.
The code I posted will do the resize part... It should be a matter of 5 minutes, and a few brain cells to throw it into a class =).
Z.
You know, I don't quite get why no one has mentioned sparse arrays.
It's THE standard approach in writing something like a spreadsheet or montrous file index that can have 100 x 10K cells -so it may not fit in memory whole. Some horrible thing like:
ie., char[100][10000][132];
But a sparse array is only partly populated, so only the 'active' cells are in memory. This is a very strightforward way to deal with n-dimensional arrays as virtual entities. And you don't have to worry about memory leaks because you can find every entity and use delete or free() depending on how it was allocated.