two dimensional array, size determined dynamically
Hi
I want to define a two dimensional array whose size N,M is not known at compile time.
I know one way: define an array of pointers to an array of pointers and then write a little loop that does the allocation.
However this looks clumsy to me.
The following does not work
int array[][] = new int[N][M];
Is new int [N][M] acceptable as an expression, if so what would the left hand side look like?
Re: two dimensional array, size determined dynamically
try:
int* x(int m, int n)
{
return new int[m][n];
}
Re: two dimensional array, size determined dynamically
Peppy,
Your x function does not compile.
Re: two dimensional array, size determined dynamically
By the way I found a forum discussion that provides suggestions, which are basically that you cannot get around programming with pointers of pointers and a for loop as in this:
I believe you have to create an array of pointers to an array of ints to get this to work. Here is an example:
Code:
int **array_ptr; //two * are needed because it is a pointer to a pointer
array_ptr=new int*[firstnumber]; //creates a new array of pointers to int objects
for(int i=0; i<firstnumber; ++i)
array_ptr[i]=new int[secondnumber];
//now, you can access the members like you can with normal 2d arrays
//like array_ptr[1][3]
The nice thing is that you can access the array members as if it were a 2d array
Re: two dimensional array, size determined dynamically
I would like to add that it seems that this code using int's can be generalised to arbitrary data types and that we should be able to make use of templates.
Re: two dimensional array, size determined dynamically
I suggest to incorporate that in a class (template or not)
And careful how you do the "cleanup" (you need to iterate through the "first dimension" delete[] the pointer to type, and after the loop delete[] name of the first_dimension_pointer)
Or use std::vector<std::vector<your_type> > or QVector<QVector<your_type> > This is the recommended way to achieve two dimension "array"
Re: two dimensional array, size determined dynamically
Another simple solution is to just use a 1D array:
Code:
int* array = new int[rows * columns];
and do the 2D indexing yourself, which is very simple.
This approach guarantees that your allocated storage occupies a single, contiguous chunk of memory, something that may be good, bad or indifferent depending on exactly what you're doing. It is probably slightly faster than performing the multiple, small allocations required by the 2D case.
Re: two dimensional array, size determined dynamically
Thankyou for the suggestions concerning cleanup , vectors and the use of 1d array.
Re: two dimensional array, size determined dynamically
Here is my attempt at sample code that uses a class and double allocation.
Code:
#include <QtCore/QCoreApplication>
#include <QtDebug>
template <typename T> class DynArray2D
{
public:
DynArray2D(int n, int m)
{
_n = n;
_array = new T*[n];
for(int i = 0; i < n; i++)
_array[i]= new T[m];
}
void setValue(int n, int m, T v){_array[n][m]=v;}
T getValue(int n, int m){return _array[n][m];}
~DynArray2D(){
for(int i=0; i < _n;i++)
delete [] _array[i];
delete [] _array;
}
private:
T **_array;
int _n;
};
int main(int argc, char *argv[])
{
DynArray2D<int> dynIntArray(3,4);
dynIntArray.setValue(1,2,666);
qDebug()<< dynIntArray.getValue(1,2);
return a.exec();
}
Re: two dimensional array, size determined dynamically
Thanks for sharing the code
But i have some little comment, you don't store the second "dimension" value so if you have to iterate the array, you need to remember the size
Anyway, i agree with what zlatomir said... you can use std::vector or QVector, these are allready tested, they don't leak, have great performance and integrate well with algorithms (stl or qt)