CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Dynamic Two Dimensional Array

1. Junior Member
Join Date
Mar 2009
Posts
2

## Dynamic Two Dimensional Array

template <typename T>
T** AllocateDynamicArray(int Rows , int cols)
{
T** DynamicArray;
DynamicArray = new T*[Rows];
for(int i = 0; i < Rows ; i++)
{
DynamicArray[i] = new T [cols];
return DynamicArray;
}
}

template <typename T>
void FreeDynamicArray(T** dArray)
{
delete []* dArray;

delete [] dArray;
}

i used above code to allocate two dimentional array

double **a = AllcateDyamicArray<double>(n,m);

.
.

.

FreeDynamicArray<double>(a);

this is one method

and second is

vector<vector<double>> v(n,vector<double>(m,0));

which is good method to allocate two dimensional array. b'coz i my project lot of Matrix operation are required.
so please suggest me any good method.

2. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Dynamic Two Dimensional Array

The vector approach has no disadvantage over your home-grown one. However, an option which might be better is the contiguous-memory approach:
Code:
vector<double> v_data(n*m);
vector<double*> v(n);
for (int i = 0; i < n; i++)
v[i] = &v_data[i*m];
You may also wish to investigate the Matrix Template Library. I haven't used it, but it claims high performance and certainly looks decently-designed at first glance.

3. Senior Member
Join Date
Jan 2009
Posts
1,689

## Re: Dynamic Two Dimensional Array

Originally Posted by NandiniC
vector<vector<double>> v(n,vector<double>(m,0));
Just FYI, this is dangerous to do with the GNU compiler. I've run into an issue where the grammar will read >> as a bit shift for some reason. Since then I always do 2D vectors like this:

Code:
typedef vector<double> DoubleVector;
vector<DoubleVector> v

4. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Dynamic Two Dimensional Array

You can simply include a space between the two >s. I usually do.

5. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,748

## Re: Dynamic Two Dimensional Array

Originally Posted by ninja9578
Just FYI, this is dangerous to do with the GNU compiler. I've run into an issue where the grammar will read >> as a bit shift for some reason.
Yes, this problem is not just with g++, but with any standard compliant C++ compiler prior to the advent of C++0x where this greedy matching will finally be changed. The typedef can be useful more than as just a way of avoiding this, but otherwise insertion of a space will do: vector<vector<double> >.

6. ## Re: Dynamic Two Dimensional Array

Visual Studio 2008 is happy with >>

7. Senior Member
Join Date
Jan 2009
Posts
1,689

## Re: Dynamic Two Dimensional Array

Originally Posted by Lindley
You can simply include a space between the two >s. I usually do.
Originally Posted by laserlight
Yes, this problem is not just with g++, but with any standard compliant C++ compiler prior to the advent of C++0x where this greedy matching will finally be changed. The typedef can be useful more than as just a way of avoiding this, but otherwise insertion of a space will do: vector<vector<double> >.
Huh, I don't know why I never thought to try that. Oh well, I like the typedef better anyway, looks neater and easier to read.

8. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Dynamic Two Dimensional Array

Originally Posted by NandiniC
template <typename T>
T** AllocateDynamicArray(int Rows , int cols)
{
T** DynamicArray;
DynamicArray = new T*[Rows];
for(int i = 0; i < Rows ; i++)
{
DynamicArray[i] = new T [cols];
return DynamicArray;
}
}
What if you had 10,000 rows and 10,000 columns? You are calling "new" 10,001 times. That could lead to memory fragmentation.

Unless there is a specific reason to dynamically allocate each row, the code you posted only needs two calls to "new". One call allocates the pointers, and the second call allocates the pool of data. Then you loop, pointing the pointers in the memory pool:
Code:
template <typename T>
T** AllocateDynamicArray(int Rows , int cols)
{
T** DynamicArray;
DynamicArray = new T*[Rows];
T* pool = new T[Rows*cols];
for(int i = 0; i < Rows ; i++)
{
DynamicArray[i] = pool;
pool += cols;
}
return DynamicArray;
}
Now it doesn't matter how many rows or columns you have, you still only make two calls to "new". Not only that, I expect this code to be much faster than calling the allocator cols+Rows times.

On destruction, you have only two calls to delete to make:
Code:
template <typename T>
void FreeDynamicArray(T** dArray)
{
delete [] dArray[0];
delete [] dArray;
}
Since dArray[0] pointed to the original start of the pool, you delete that, then you delete the row pointers. Just two calls to delete, regardless of the number of rows and columns.

Regards,

Paul McKenzie

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•