Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multi-dimensional heap array

Reply
Thread Tools

Multi-dimensional heap array

 
 
JKop
Guest
Posts: n/a
 
      09-04-2004
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
 
Reply With Quote
 
 
 
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-04-2004
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)
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      09-04-2004
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


-JKop
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-04-2004
JKop wrote:

> 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.




int (*p)[3]=new int[2][3];

// ...


delete[] p;






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-04-2004
Derrick Coetzee wrote:

> JKop wrote:
>
>> 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
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      09-04-2004
JKop <> wrote in news:IXl_c.26563$:

> 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?
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-04-2004
Andre Kostur posted:

> JKop <> wrote in news:IXl_c.26563$Z14.8409

@news.indigo.ie:
>
>> 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];
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-04-2004
Ioannis Vranos wrote:

> 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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-06-2004
Ioannis Vranos <> wrote:
> Derrick Coetzee wrote:
>
> > JKop wrote:
> >
> >> 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

>
> > 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!


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How much heap can I reserve for JVM? Error when allocation very much heap Raymond Schanks Java 0 04-11-2010 04:25 PM
Array based Binary Heap in C arnuld C Programming 19 11-07-2009 03:07 PM
stl heap question: restoring heap validinty after changing 1 element viki C++ 6 06-28-2008 10:12 AM
Heap dump file size vs heap size Michal Slocinski Java 1 03-25-2008 12:54 PM
Confusing array garbage collection, how to get valid heap snapshot? Greg Fodor Ruby 1 05-23-2007 02:50 AM



Advertisments