Release memory using 'delete' operator for 2 dimensional array
-
I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh
-
I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh
Seems to be but in your second loop you are using i and not j as you probably should, that!s just a copy-paste problem in the post here i supose...
for(int j = 0; j<4; j++)
{
delete []a[i]; // here you wrote i, not j, that might cause problems if this is actually in your code
}> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh
as addendum to Code-o-mat, you may use (instead of hard-wired limits)
for (int j=0; j< sizeof(a)/sizeof(a[0]); j++)
{
delete [] a[j];
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
as addendum to Code-o-mat, you may use (instead of hard-wired limits)
for (int j=0; j< sizeof(a)/sizeof(a[0]); j++)
{
delete [] a[j];
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Yeah, i wonder why C++ doesn't have something like an
itemcountof
or such operator for that by default. Many times it would be useful, i keep making macros for sizeof(array)/sizeof(array[0]) and i keep forgetting where i put them. :)> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh
Nikesh Jagtap wrote:
int a[4][6] which is allocated dynamically.
Not unless you've declared it as:
int **a = new int*[4];
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch