func new

Discussion in 'C++' started by Dabroz, Jul 29, 2003.

  1. Dabroz

    Dabroz Guest

    can i allocate memory in multidimensional arrays by new?

    for example:

    int* something;
    something=new int[20][20][20];

    it doesn't work! how i can make that?

    - dabroz - []

    "The emperor is a rotting shell, holding together a long dead empire with
    fetid dreams and lies. Will you listen to them or embrace Chaos?"
    Dabroz, Jul 29, 2003
    #1
    1. Advertising

  2. On Tue, 29 Jul 2003 12:13:34 +0200, "Dabroz" <> wrote:

    >can i allocate memory in multidimensional arrays by new?


    Yes.


    >for example:
    >
    >int* something;
    >something=new int[20][20][20];
    >
    >it doesn't work! how i can make that?


    It does not compile because the type of the 'new' expression is
    not


    int*


    but


    int (*)[20][20]


    Declare your pointer as


    int (*something)[20][20];


    and it will compile.

    But better, if you absolutely must program at the lowest
    level instead of using the standard library, 'typedef' like so:


    int main()
    {
    struct Tensor
    {
    int elem[20][20][20];
    };

    Tensor* t = new Tensor;

    t->elem[1][2][3] = 666;

    delete t;
    }


    General recommendation is, however, to use standard library and
    boost collection classes.

    Hth.
    Alf P. Steinbach, Jul 31, 2003
    #2
    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. CRON
    Replies:
    24
    Views:
    200,655
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    453
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    501
    Hari Sekhon
    Jun 20, 2006
  4. Vinko Vrsalovic

    int func() v/s int func(void)

    Vinko Vrsalovic, Jan 21, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,304
    Villy Kruse
    Jan 24, 2005
  5. Replies:
    37
    Views:
    463
Loading...

Share This Page