func new

D

Dabroz

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 - [[email protected]]

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

Alf P. Steinbach

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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top