470,815 Members | 1,427 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

How should be delete for this allocation

Hello, I have a array of double pointers as shown below

dummy_class **ptr[10] ; //assume dummy class to be some c++ class
I have allocated it like this:

for(int i = 0 ; i<10 ; i++)
{
ptr[i] = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[i][j] = new dummy_class ;
}
}

1. Whether the above allocation is valid
2. How will be delete statements for this.

Jun 29 '06 #1
9 1317
pa******@gmail.com wrote:
Hello, I have a array of double pointers as shown below

dummy_class **ptr[10] ; //assume dummy class to be some c++ class
I have allocated it like this:

for(int i = 0 ; i<10 ; i++)
{
ptr[i] = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[i][j] = new dummy_class ;
}
}

1. Whether the above allocation is valid
2. How will be delete statements for this.


1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];
}

Jun 29 '06 #2
pa******@gmail.com wrote:
Hello, I have a array of double pointers as shown below
<snip code>
1. Whether the above allocation is valid
2. How will be delete statements for this.


Is this homework?

Stewart
Jun 29 '06 #3

Ron Natalie wrote:


1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];
}


Thank you. But is it not possible to delete using delete[] ?

Jun 29 '06 #4
pa******@gmail.com wrote:
Ron Natalie wrote:


1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];
}


Thank you. But is it not possible to delete using delete[] ?


Ron made a mistake. If you allocated using 'new[]', you should
deallocate using 'delete[]'. The proper deallocation code would
be

for(int i = 0 ; i<10 ; i++) {
for(int j=0 ; j<5 ; j++)
delete ptr[i][j];
delete[] ptr[i];
}

When my OCD is acting up, I even rewrite the loops so that the
deallocations are in the reverse order of allocations:
for(int i = 10 ; i-- >0;) {
for(int j=5 ; j-- >0;)
delete ptr[i][j];
delete[] ptr[i];
}

(I hope I got the indexing correctly :-])

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #5
pa******@gmail.com wrote:
Ron Natalie wrote:

1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];
}

Thank you. But is it not possible to delete using delete[] ?


Ron's code contained an error - it should be:

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete[] ptr[i];
}

Every new you write should have a matching delete, and every new[] and
matching delete[].

Tom
Jun 29 '06 #6
Ron Natalie wrote:
pa******@gmail.com wrote:
Hello, I have a array of double pointers as shown below

dummy_class **ptr[10] ; //assume dummy class to be some c++ class
I have allocated it like this:

for(int i = 0 ; i<10 ; i++)
{
ptr[i] = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[i][j] = new dummy_class ;
}
}

1. Whether the above allocation is valid
2. How will be delete statements for this.

1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];


delete[] ptr[i];
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #7
Thank you everyone for your kind help.

Jun 29 '06 #8

8 posts already in this thread and no-one has suggested that the OP use
std::vector. You guys must be going soft !

Jun 29 '06 #9
pa******@gmail.com wrote:
Ron Natalie wrote:
1, Looks OK to me.
2. The deallocation needs to mirror the allocations.

for(i = 0; i < 10; ++i) {
for(int j = 0; j < 5; ++j)
delete ptr[i][j];
delete ptr[i];
}


Thank you. But is it not possible to delete using delete[] ?


Actually the last one should be delete[] my mistake.
Jun 29 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Spikinsson | last post: by
20 posts views Thread by Ioannis Vranos | last post: by
15 posts views Thread by Roy Smith | last post: by
7 posts views Thread by morz | last post: by
27 posts views Thread by fermisoft | last post: by
9 posts views Thread by rohits123 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.