| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Silver" |
| Date: |
23 Nov 2003 08:41:20 AM |
| Object: |
Dynamic 2-D array - How ??? |
Hi everyone,
I want to write a class with an NxM array as a private member. I want the
memory for this array to be dynamically allocated. I'm a bit confused...
I would like to know how this can be done both with malloc/free and
new/delete.
(Btw, is there any case where malloc/free is better than new/delete?)
Here's what I though (code follows)
Thanks
class A
{
private:
float** ppa;
int fl; //flag
public:
A(int n, int m); // Constructor
A(); // Default constructor
~A(); // Destructor
};
A::A(int n, int m) {
cout << endl << "Constructor for class A called.";
fl = 1;
ppa = (float **)malloc(n * sizeof(float *));
for(int i = 0; i < n; i++)
ppa[i] = (float *)malloc(m * sizeof(float));
}
A::A() {
cout << endl << "Default constructor for class A called.";
fl = 0;
}
A::~A() {
if(fl) {
free((void **)cpp);
cout << endl << "Destruvtor for class A called.";
}
}
.
|
|
| User: "Unforgiven" |
|
| Title: Re: Dynamic 2-D array - How ??? |
23 Nov 2003 12:00:39 PM |
|
|
Silver wrote:
Hi everyone,
I want to write a class with an NxM array as a private member. I want
the memory for this array to be dynamically allocated. I'm a bit
confused... I would like to know how this can be done both with
malloc/free and new/delete.
The approach you use below is basically right. Here's the same thing with
new/delete:
float **ppa = new float*[n];
for( int i = 0; i < n; ++i )
ppa[i] = new float[m];
And when deleting:
for( int i = 0; i < n; ++i )
delete[] ppa[i];
delete[] ppa;
(Btw, is there any case where malloc/free is better than new/delete?)
No.
Also, a better way to do it is this:
std::vector<std::vector<float> > float_array(n, std::vector<float>(m,
0.0f));
You can also dynamically resize it with this solution, and you don't need to
take care of cleanup.
--
Unforgiven
"Most people make generalisations"
Freek de Jonge
.
|
|
|
| User: "foo" |
|
| Title: Re: Dynamic 2-D array - How ??? |
23 Nov 2003 06:25:56 PM |
|
|
"Unforgiven" <jaapd3000@hotmail.com> wrote in message news:<bpqskb$1qo21u$1@ID-136341.news.uni-berlin.de>...
Silver wrote:
Hi everyone,
I want to write a class with an NxM array as a private member. I want
the memory for this array to be dynamically allocated. I'm a bit
confused... I would like to know how this can be done both with
malloc/free and new/delete.
The approach you use below is basically right. Here's the same thing with
new/delete:
float **ppa = new float*[n];
for( int i = 0; i < n; ++i )
ppa[i] = new float[m];
And when deleting:
for( int i = 0; i < n; ++i )
delete[] ppa[i];
delete[] ppa;
(Btw, is there any case where malloc/free is better than new/delete?)
No.
Also, a better way to do it is this:
std::vector<std::vector<float> > float_array(n, std::vector<float>(m,
0.0f));
You can also dynamically resize it with this solution, and you don't need to
take care of cleanup.
Here's a cleaner method for 2D Arrays.
template < class T>
class dynamic_2d_array
{
public:
dynamic_2d_array(int row, int col):m_row(row),m_col(col),
m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
~dynamic_2d_array(){if(m_data) delete []m_data;}
inline T* operator[](int i) {return (m_data + (m_col*i));}
inline T const*const operator[](int i) const {return (m_data +
(m_col*i));}
private:
const int m_row;
const int m_col;
T* m_data;
};
See following link for more information:
http://www.axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9
.
|
|
|
|
|

|
Related Articles |
|
|