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];
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
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
thanx
To delete that memory:
Code:for(int i=0;i<10;i++) delete p[i]; delete [] p;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
>> delete [] p[i];
nice catch
yes...it should be
Code:delete[] p[i];
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
Oops. Yes.
Code:delete [] p[i];
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Sometimes oops just isnt good enough.
maybe oops[100] would do.
Perhaps you were thinking of [50]. 10*5 = 50.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.