473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic allocation of a 2 dimensional array

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
Jul 22 '05 #1
4 8490
CodeMedic wrote:

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[i];

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
CodeMedic wrote:
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:


please don't mix calloc and delete.

http://www.parashift.com/c++-faq-lit...html#faq-16.15

might give you, what you intended.

IB
Jul 22 '05 #3
Ingo Buescher <us*********@r-arcology.de> wrote in message news:<c8**********@fbi-news.cs.Uni-Dortmund.DE>...
CodeMedic wrote:
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:


please don't mix calloc and delete.

http://www.parashift.com/c++-faq-lit...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
Jul 22 '05 #4
foo
po******@hotmail.com (CodeMedic) wrote in message news:<89**************************@posting.google. com>...
Ingo Buescher <us*********@r-arcology.de> wrote in message news:<c8**********@fbi-news.cs.Uni-Dortmund.DE>...
CodeMedic wrote:
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:


please don't mix calloc and delete.

http://www.parashift.com/c++-faq-lit...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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
6
by: R.kumaran | last post by:
hello all I have a problem in allocating memory for a pointer to an array. this is my code void main() { int(*a); *a=(int *)malloc(2*2*sizeof(int)); ........
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
11
by: gianluca | last post by:
Hy list, I've to declare a 3D matrix in C . I tried with that code: double ***mat; mat=(double***)G_malloc(ndimension*(sizeof(double)); but the program send me a run time error.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.