By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Dynamic 2-D array - How ???

P: n/a
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.";

}

}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
foo
"Unforgiven" <ja*******@hotmail.com> wrote in message news:<bp*************@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?T..._ID=4&CAT_ID=9
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.