473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1438
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2727
by: Spikinsson | last post by:
I create a new char and when I attempt to delete it, the debug gives this error (during runtime): Debug Error! Program: xxx DAMAGE: after Normal block (#58) at 0x00C315A0.
20
4116
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
15
2362
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
4
2024
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
7
16424
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
27
7152
by: fermisoft | last post by:
I have a strange problem in my project. I have a class like this...... class CMsg { public: void *m_body; ~CMsg()
9
2904
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
9
8147
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
23
1914
by: Martin T. | last post by:
Hi all! char* p = new char; // POD! .... delete p; // std compliant delete p; // will work on VC8 free(p); // will also work on VC8 I am interested on which platforms/compilers the...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.