I want to create a dynamic 2 dimensional array like this
Why dosen't new allow this? shouldn't it be possible for new to allocate memory the size of x*y and return a pointer to z[0][0]?Code:parray = new z[x][y];
Printable View
I want to create a dynamic 2 dimensional array like this
Why dosen't new allow this? shouldn't it be possible for new to allocate memory the size of x*y and return a pointer to z[0][0]?Code:parray = new z[x][y];
Example of 2D memory allocation
make sure u delete the mempry after useCode:int **ptr;
ptr=new int *[10];//creates an array pointer
for(int i=0;i<10;i++)
p[i]=new int [5];//creates space for each row
thanx
To delete that memory:
Code:for(int i=0;i<10;i++)
delete p[i];
delete [] p;
Not quite. Should be:Code:for(int i=0;i<10;i++)
delete [] p[i];
delete [] p;
I think what dwks is saying is right
>> delete [] p[i];
nice catch
yes...it should be
Code:delete[] p[i];
Oops. Yes.
Code:delete [] p[i];
Sometimes oops just isnt good enough.
maybe oops[100] would do. :)
Perhaps you were thinking of [50]. 10*5 = 50.