mano M wrote:
> Can we allocate 2D array (dynamically) in one line.
>    
>   int **p;
>    
>   int i,j;
>   std::cin>>i>>j;
>    
>   p=new int[i][j];
>    
>   ----------------
>   ------------------
>   ----------------
>    
>   delete [][]p;
>    
>    
>   Is the above code snippet acceptable?
>   Thanks,
>   Manoj

You know you can always try stuff out and see what happens instead of 
waiting for a response from a mailing list.

But, no, you can't do it that way.  However, you CAN do two allocations 
and then split up the memory into smaller chunks (by row):

int **p, *p2, *p3;
size_t i, j, k;

std::cin >> i >> j;
p3 = p2 = new int[i * j];
p = new int *[i];
for (k = 0; k < i; k++)
{
   p[k] = p3;
   p3 += j;
}

...do stuff with 2D array as usual...

delete [] p;
delete [] p2;


However, you shouldn't have new/delete at the application layer.  The 
above is just an example.  Use a library layer template in production code.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to