Question about delete[]

R

Randy Gordon

Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
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.
 
M

Moonlit

Randy Gordon said:
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
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
 
G

Gianni Mariani

Randy said:
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
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
 
T

Thomas J. Clancy

Randy Gordon said:
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
delete[] names;


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

tom
 
R

Randy Gordon

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;
}

and not this:

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

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

Max M.

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

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

and not this:

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



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
 
L

lilburne

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

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

and not this:

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

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;' 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;' when the elements are
PODs like char, but it is not correct.
 
P

Peter van Merkerk

Say I do the following:
char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
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.
 
S

Stuart Golodetz

Randy Gordon said:
Say I do the following:

char **names = new char*[100];
for (int i = 0; i < 100; i++) {
names = 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;
}
delete[] names;


for (int i=0; i<100; ++i)
{
delete [] names; // 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.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top