Dynamic 2-D array - How ???

S

Silver

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 = (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.";

}

}
 
U

Unforgiven

Silver said:
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 = new float[m];

And when deleting:
for( int i = 0; i < n; ++i )
delete[] ppa;

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.
 
F

foo

Unforgiven said:
Silver said:
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 = new float[m];

And when deleting:
for( int i = 0; i < n; ++i )
delete[] ppa;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top