By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

How should be delete for this allocation

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thank you everyone for your kind help.

Jun 29 '06 #8

P: n/a

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

P: n/a
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.