Dynamic 2-D array - How ???



 DEVELOP > c-Plus-Plus > Dynamic 2-D array - How ???

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.



  Page 1 of 1

1

 


Related Articles
How can you quickly find the number of elements stored in a a) staticarray b) dynamic array ?
How can I increment/decrement a multidemensional array without a loop?
how to declare array of class pointer
Help me please, how can I create an array of object of a my class?
how to sort an array of objects?
How to create an N dimensional array with N elements?
How to make a C++ array which have size bigger than 32767
how to "new" a two-dimension array in C++?
How to zero-initialize a C string (array of wchar_t)?
how can i use array to be the object in class and call the member function?
How to return an array of own datatype
How to let a function return an array
How to pass a pointer to an unknown-size array?
How to make sure that all the values in an array are different?
How to minupulate the elements (counting by 3) of an array usingSTL?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER