How should be delete for this allocation

P

pavan734

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 = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[j] = new dummy_class ;
}
}

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

Ron Natalie

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 = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[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[j];
delete ptr;
}
 
P

pavan734

Ron said:
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[j];
delete ptr;
}


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

Victor Bazarov

Ron said:
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[j];
delete ptr;
}


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[j];
delete[] ptr;
}

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[j];
delete[] ptr;
}

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

V
 
T

Tom Widmer

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[j];
delete ptr;
}



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[j];
delete[] ptr;
}

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

Tom
 
V

Victor Bazarov

Ron said:
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 = new dummy_class* [5] ;
for(int j=0 ; j<5 ; j++) // This 5 is because of previous
allocation of 5
{
ptr[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[j];
delete ptr;


delete[] ptr;

V
 
T

tragomaskhalos

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

Ron Natalie

Ron said:
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[j];
delete ptr;
}


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


Actually the last one should be delete[] my mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top