-
March 18th, 2009, 10:55 PM
#1
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.
-
March 18th, 2009, 11:04 PM
#2
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.
-
March 19th, 2009, 08:08 AM
#3
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
-
March 19th, 2009, 08:12 AM
#4
Re: Dynamic Two Dimensional Array
You can simply include a space between the two >s. I usually do.
-
March 19th, 2009, 08:14 AM
#5
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> >.
-
March 19th, 2009, 11:31 AM
#6
Re: Dynamic Two Dimensional Array
Visual Studio 2008 is happy with >>
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 19th, 2009, 01:48 PM
#7
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.
-
March 19th, 2009, 06:38 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|