Dynamic allocation of a 2 dimensional array

C

CodeMedic

To anyone that is more intelligent than me or just knows better, I
could use some help.

I am attemping to write a simple program that will created a random
maze that will only require for information the cell length and height
of the maze. I am going to use a dynamicly created 2 dimensional array
based on the size parameters given. This is what I have currently:

// Constructor takes the two size parmeters and builds the array.
Maze::Maze (int cRowSize, int cColumnSize)
{
RowSize = cRowSize;
ColumnSize = cColumnSize;
MazeArray = (MazeCell*)calloc((RowSize*ColumnSize),
sizeof(MazeCell));
}

//By the time the constructor is finish each pointer in the array will
have an object or will be null
//not sure ifI need to use delete, free or both
Maze::~Maze (void)
{
int Row = RowSize;
int Column = ColumnSize;
for (;Row > 0 ;Row--)
{
for (;Column > 0;Column--)
delete MazeArray[Row][Column];
}
free(MazeArray);
}

I get this error:

no match for `MazeCell& [int&]' operator


I have had such problems trying to do this is the past. I understand
that I can create a 1D array and then fill each space with another
array but I feel that there could be a better way. Any and all help on
what is seen here would be greatly appreciated.

BTW I am using Xcode on OSX 10.3

CodeMedic
cert# 101010
 
K

Karl Heinz Buchegger

CodeMedic said:
To anyone that is more intelligent than me or just knows better, I
could use some help.

I am attemping to write a simple program that will created a random
maze that will only require for information the cell length and height
of the maze. I am going to use a dynamicly created 2 dimensional array
based on the size parameters given. This is what I have currently:

// Constructor takes the two size parmeters and builds the array.
Maze::Maze (int cRowSize, int cColumnSize)
{
RowSize = cRowSize;
ColumnSize = cColumnSize;
MazeArray = (MazeCell*)calloc((RowSize*ColumnSize),
sizeof(MazeCell));
}

//By the time the constructor is finish each pointer in the array will
have an object or will be null
//not sure ifI need to use delete, free or both
Maze::~Maze (void)
{
int Row = RowSize;
int Column = ColumnSize;
for (;Row > 0 ;Row--)
{
for (;Column > 0;Column--)
delete MazeArray[Row][Column];
}
free(MazeArray);
}

I get this error:

no match for `MazeCell& [int&]' operator

MazeArray is not an array of pointers to pointers.
It is a plain vanilla array of pointers

for( i = 0; i < RowSize*ColumnSize; ++i )
delete MazeArray;

free(MazeArray);
I have had such problems trying to do this is the past.

It is simple:

malloc or calloc goes with free
new goes with delete
new [] goes with delete []

If you are working in C++ then there is no need to use
malloc or calloc (other then for interfacing to C code).
Just stick with new and new[] and forget about malloc or
calloc or free.
 
F

foo

Ingo Buescher said:
please don't mix calloc and delete.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

might give you, what you intended.

IB

You are indeed correct, that website helpped, not exactly where I was
going but just about equal. Thanks 8-].

CodeMedic
cert#101010


I recommend using the following class instead.

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(const
dynamic_2d_array&src):m_row(src.m_row),m_col(src.m_col),
m_data((src.m_row!=0&&src.m_col!=0)?new T[src.m_row*src.m_col]:NULL){
for(int r=0;r<m_row;++r)for(int c=0;c<m_col;++c) (*this)[r][c] =
src[r][c];
}
~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:
dynamic_2d_array& operator=(const dynamic_2d_array&);
const int m_row;
const int m_col;
T* m_data;
};


Download it via following link:
www.axter.com/code/dynamic_2d_array.h
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top