473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Question about delete[]

Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it?
Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;

or like this:

delete[] names;

Basically, does delete[] call each individual delete or not? If so, what
happens if I do the former method? Assuming it doesn't, then I guess the
second method would create a memory leak.
Jul 19 '05 #1
8 2505

"Randy Gordon" <no****@forme.com> wrote in message
news:3_****************@newssvr13.news.prodigy.com ...
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it? Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;

or like this:

delete[] names;
Both. You allocated the array of pointers and then an array of characters in
the loop. So you have to delete those arrays too.

Basically, does delete[] call each individual delete or not? If so, what No. happens if I do the former method? Assuming it doesn't, then I guess the
second method would create a memory leak.
If you leave one delete out you create a memory leak. For every new there
has to be a corresponding delete.


Regards, Ron AF Greve
Jul 19 '05 #2
Randy Gordon wrote:
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it?
Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;

or like this:

delete[] names;

Basically, does delete[] call each individual delete or not?
No - it does not. The basic idea is that if you call new, you're
probably goint to need to call delete.

If so, what happens if I do the former method? Assuming it doesn't, then I guess the
second method would create a memory leak.


Yes - it leaks.

You can use a "smart pointer" class that does the delete for you.

G

Jul 19 '05 #3
"Randy Gordon" <no****@forme.com> wrote in message
news:3_****************@newssvr13.news.prodigy.com ...
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it? Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;


Simple rule of thumb... delete what you create (new) unless you're using
smart pointers or auto pointers.

tom


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.537 / Virus Database: 332 - Release Date: 11/6/2003
Jul 19 '05 #4
> Both. You allocated the array of pointers and then an array of characters
in
the loop. So you have to delete those arrays too.

Basically, does delete[] call each individual delete or not? If so, what

No.
happens if I do the former method? Assuming it doesn't, then I guess the
second method would create a memory leak.


If you leave one delete out you create a memory leak. For every new there
has to be a corresponding delete.


Regards, Ron AF Greve


All right, that makes sense. Now that I think about, why is the following
correct:

for (int i = 0; i < 100; i++) {
delete names[i];
}

and not this:

for (int i = 0; i < 100; i++) {
delete[] names[i];
}

I mean, it's an array of characters I'm trying to free right? So shouldn't I
use delete[] instead of delete?
Jul 19 '05 #5
Randy Gordon wrote:
All right, that makes sense. Now that I think about, why is the following
correct:

for (int i = 0; i < 100; i++) {
delete names[i];
}

and not this:

for (int i = 0; i < 100; i++) {
delete[] names[i];
}

In fact, the latter is correct; the former is not.

I mean, it's an array of characters I'm trying to free right? So shouldn't
I use delete[] instead of delete?


You should. You must, actually.

Max




Jul 19 '05 #6
Randy Gordon wrote:

All right, that makes sense. Now that I think about, why is the following
correct:

for (int i = 0; i < 100; i++) {
delete names[i];
}

and not this:

for (int i = 0; i < 100; i++) {
delete[] names[i];
}

I mean, it's an array of characters I'm trying to free right? So shouldn't I
use delete[] instead of delete?


Yes the second version 'delete[] names[i];' is the correct
version to use if you are deleting an array, because it
calls the destructor for each element in the array. However,
a lot of people use 'delete names[i];' when the elements are
PODs like char, but it is not correct.

Jul 19 '05 #7
> Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it? Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;

or like this:

delete[] names;

Basically, does delete[] call each individual delete or not? If so, what happens if I do the former method? Assuming it doesn't, then I guess the second method would create a memory leak.


Others have already answered your question. But I think you should know
that your code will become a lot simpler and safer when you replace the
C style arrays with the C++ classes std::vector and st::string. The code
you posted for allocating and deallocating a collection of names could
be reduced to:

std::vector<std::string> names(100);

Not only is this code shorter, but both std::vector and std::string can
(easilly) dynamically grow to accommodate more names and/or longer names
(i.e. no arbitrary length limitation). When the names object runs out of
scope (e.g. because of function return or exception) all memory
allocated on its behalf will automatically be freed. If you have to
delete the memory manually like in the code you posted, it can be rather
tricky not to leak memory when an exception occures.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #8
"Randy Gordon" <no****@forme.com> wrote in message
news:3_****************@newssvr13.news.prodigy.com ...
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names[i] = new char[5];
}

When I'm done with the array of character pointers, how should I delete it? Like this:

for (int i = 0; i < 100; i++) {
delete names[i];
}
delete[] names;
for (int i=0; i<100; ++i)
{
delete [] names[i]; // note: delete [], *not* delete
}
delete [] names;

HTH,

Stuart.
or like this:

delete[] names;

Basically, does delete[] call each individual delete or not? If so, what
happens if I do the former method? Assuming it doesn't, then I guess the
second method would create a memory leak.

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: J | last post by:
Dear Advanced users, Here are parts of a code sample and I need to find out a few things from this. Please tell me your input. I am supposed to get others help as well to make the answers as...
6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
4
by: WertmanTheMad | last post by:
Ok here goes, another odd SQL Question ....as always.. How do I get ... The value of a paramater passed to say a Trigger OR The SQL Itself Like this , say I have a Trigger set on delete, ...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
7
by: subramanian100in | last post by:
The following discussion is for learning purpose only. 1) Suppose for a type T, I have T *ptr = new T; To free ptr, suppose I use ( I deliberately omit in delete.) delete ptr;
0
by: topmind | last post by:
siddharthkh...@hotmail.com wrote: Good luck. The behavior and conventions of web versus fat-client (or paper-oriented) reports are so different that making a generic anything that serves both of...
0
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.