Multi-dimensional heap array

J

JKop

int main()
{
int normal[2][3];

int (&on_the_heap)[2][3] = *new int[2][3];
}


My operator precendence may be wrong in the above.


-JKop
 
J

JKop

Derrick Coetzee posted:
JKop said:
int (&on_the_heap)[2][3] = *new int[2][3];

There is no language construct for dynamically allocating
multidimensional arrays. See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html

(Substitute new for malloc appropriately)

I've done it before. I used a reference or a pointer to a
multidimensional array I believe.

Something along the lines of:

int k[8];

int (&r)[2] = k;

r[0][1] = 1;
r[0][2] = 2;
r[0][3] = 3;
r[0][4] = 4;
r[1][1] = 5;
r[1][2] = 6;

//and so on


-JKop
 
I

Ioannis Vranos

Derrick said:
JKop said:
int (&on_the_heap)[2][3] = *new int[2][3];


There is no language construct for dynamically allocating
multidimensional arrays.

What?





See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html


It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!


In any case in C it can be done:


#include <stdlib.h>

/* ... */

int (*p)[3]=malloc(2*sizeof(*p));

/* ... */

free(p);






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
A

Andre Kostur

JKop said:
Derrick Coetzee posted:
JKop said:
int (&on_the_heap)[2][3] = *new int[2][3];

There is no language construct for dynamically allocating
multidimensional arrays. See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html

(Substitute new for malloc appropriately)

I've done it before. I used a reference or a pointer to a
multidimensional array I believe.

Something along the lines of:

int k[8];

int (&r)[2] = k;

r[0][1] = 1;
r[0][2] = 2;
r[0][3] = 3;
r[0][4] = 4;
r[1][1] = 5;
r[1][2] = 6;

//and so on

And what does this have to do with dynamically allocating
multidimensional arrays?
 
J

JKop

Andre Kostur posted:
Derrick Coetzee posted:
JKop wrote:
int (&on_the_heap)[2][3] = *new int[2][3];

There is no language construct for dynamically allocating
multidimensional arrays. See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html

(Substitute new for malloc appropriately)

I've done it before. I used a reference or a pointer to a
multidimensional array I believe.

Something along the lines of:

int k[8];

int (&r)[2] = k;

r[0][1] = 1;
r[0][2] = 2;
r[0][3] = 3;
r[0][4] = 4;
r[1][1] = 5;
r[1][2] = 6;

//and so on

And what does this have to do with dynamically allocating
multidimensional arrays?


Do I have to do EVERYTHING myself?! Just make k dynamically
allocated:

int& k = *new int[8];
 
I

Ioannis Vranos

Ioannis said:
It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!


In any case in C it can be done:


#include <stdlib.h>

/* ... */

int (*p)[3]=malloc(2*sizeof(*p));

/* ... */

free(p);



The above implies that one dimension must be known.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
O

Old Wolf

Ioannis Vranos said:
Derrick said:
JKop said:
int (&on_the_heap)[2][3] = *new int[2][3];

There is no language construct for dynamically allocating
multidimensional arrays.

Crap, as demonstrated by JKop's example
It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!

That's been fixed in the text version of the faq (nobody bothers
to update the HTML version).
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top