# problem with allocation.

This is a discussion on problem with allocation. within the C++ Programming forums, part of the General Programming Boards category; hello. i can do that int i=5; int p=new int [i]; i want to do that int i=5,j=9; int p=new ...

1. ## problem with allocation.

hello.

i can do that

int i=5;
int p=new int [i];

i want to do that

int i=5,j=9;
int p=new int [i][j];

how can i do it?

thanks.

2. > int i=5;
> int p=new int [i];
Should be
int *p = new int[i];

> int i=5,j=9;
> int p=new int [i][j];
Should be
int **p = new int*[i];
for ( int r = 0 ; r < i ; r++ ) p[i] = new int[j];

Basically, you allocate one level of indirection at a time.

However, if 'j' was constant in your example, then you could do this
int i=5; const int j=9;
int (*p)[j] = new int[i][j];

Either way, you can use p[x][y] to access individual elements.

3. Code:
```int **p;
int x = 5;
int y = 9;

// Allocate it
p = new int*[x];

for (int i = 0; i < x; i++)
ptr[i]=new int[y];

//... Do something with it

// Delete it
for(int j = 0; j < x; j++)
delete [] ptr[j];

delete ptr;```