Multidimensional Arrays on the free store

C

Cameron

Anyone know how you put multidimensional arrays on the free store?
singles work fine but I get conversion errors when I try to specify
the second, that is...

grid_cell *cells2 = new grid_cell[64];

is fine but...

grid_cell *cells2 = new grid_cell[64][64];

gives the error...

cannot convert from 'struct grid_cell (*)[64]' to 'struct grid_cell *'

I'm guessing there is a rather simple solution to this but for the life
of me it doesn't want to present its self.
 
R

Richard Pennington

Cameron said:
Anyone know how you put multidimensional arrays on the free store?
singles work fine but I get conversion errors when I try to specify
the second, that is...

grid_cell *cells2 = new grid_cell[64];

is fine but...

grid_cell *cells2 = new grid_cell[64][64];

gives the error...

cannot convert from 'struct grid_cell (*)[64]' to 'struct grid_cell *'

I'm guessing there is a rather simple solution to this but for the life
of me it doesn't want to present its self.

Deja Vu from today on comp.language.c. ;-)

grid_cell (*cells2)[64] = new grid_cell[64][64];

-Rich
 
W

William Payne

Cameron said:
Anyone know how you put multidimensional arrays on the free store?
singles work fine but I get conversion errors when I try to specify
the second, that is...

grid_cell *cells2 = new grid_cell[64];

is fine but...

grid_cell *cells2 = new grid_cell[64][64];

gives the error...

cannot convert from 'struct grid_cell (*)[64]' to 'struct grid_cell *'

I'm guessing there is a rather simple solution to this but for the life of
me it doesn't want to present its self.

Well, say you want a two-dimensional array of chars allocated dynamically. I
would declare a pointer-to-a-pointer-to-char and allocate an array of
pointers. Then I would loop through the array an allocate space for each
pointer. When you deallocate, you loop through the elements first.

char** foo;
foo = new char*[64];
for(int i = 0; i < 64; ++i)
{
foo = new char[64];
}

/ WP
 
M

Mike Wahler

Cameron said:
Anyone know how you put multidimensional arrays on the free store?
singles work fine but I get conversion errors when I try to specify
the second, that is...

grid_cell *cells2 = new grid_cell[64];

is fine but...

grid_cell *cells2 = new grid_cell[64][64];

gives the error...

cannot convert from 'struct grid_cell (*)[64]' to 'struct grid_cell *'

I'm guessing there is a rather simple solution to this but for the life
of me it doesn't want to present its self.

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

-Mike
 
C

Cameron

Mike said:
Anyone know how you put multidimensional arrays on the free store?
singles work fine but I get conversion errors when I try to specify
the second, that is...

grid_cell *cells2 = new grid_cell[64];

is fine but...

grid_cell *cells2 = new grid_cell[64][64];

gives the error...

cannot convert from 'struct grid_cell (*)[64]' to 'struct grid_cell *'

I'm guessing there is a rather simple solution to this but for the life
of me it doesn't want to present its self.


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

-Mike
Many thanksI bookmarked that link it's just what I wanted, and thanks to
everyone else that replied :)

~Cameron
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top