Multi-dimensional heap array

Discussion in 'C++' started by JKop, Sep 4, 2004.

  1. JKop

    JKop Guest

    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
    JKop, Sep 4, 2004
    #1
    1. Advertising

  2. 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.
    Derrick Coetzee, Sep 4, 2004
    #2
    1. Advertising

  3. JKop

    JKop Guest

    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
    JKop, Sep 4, 2004
    #3
  4. 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
    Ioannis Vranos, Sep 4, 2004
    #4
  5. 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
    Ioannis Vranos, Sep 4, 2004
    #5
  6. JKop

    Andre Kostur Guest

    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?
    Andre Kostur, Sep 4, 2004
    #6
  7. JKop

    JKop Guest

    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];
    JKop, Sep 4, 2004
    #7
  8. 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
    Ioannis Vranos, Sep 4, 2004
    #8
  9. JKop

    Old Wolf Guest

    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).
    Old Wolf, Sep 6, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,151
  2. Replies:
    7
    Views:
    833
    Ryan Stewart
    Feb 20, 2005
  3. John Harrison
    Replies:
    2
    Views:
    704
    John Harrison
    Jul 14, 2003
  4. Venkat
    Replies:
    4
    Views:
    961
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page