func new

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dabroz

    func new

    can i allocate memory in multidimensiona l arrays by new?

    for example:

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

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

    - dabroz - [the_dabroz@wp.p l]

    "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?"


  • Alf P. Steinbach

    #2
    Re: func new

    On Tue, 29 Jul 2003 12:13:34 +0200, "Dabroz" <the_dabroz@wp. pl> wrote:
    [color=blue]
    >can i allocate memory in multidimensiona l arrays by new?[/color]

    Yes.

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

    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.

    Comment

    Working...