 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christian Meier Guest
|
Posted: Thu Aug 26, 2004 5:55 pm Post subject: delete |
|
|
Hi NG
I don't know if this problem is system dependend but I don't think so.
So, here is my assumption: "delete" deletes a variable. "delete[]" deletes
an array. Is this correct so far?
And now check this code out:
#include <iostream>
using namespace std;
int main()
{
char * p[100];
for(int i = 0; i < 100; ++i) {
p[i] = new char[200000];
} // for
for(int i = 0; i < 100; ++i) {
delete[] (p[i]);
//delete (p[i]); <-- this does the same on Linux with g++
} // for
}
p[i] is pointing to an array, so why doesn't it matter whether I write
"delete" with or without "[]"?
Or is this only on my system?
Thanks!
Greets Chris
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Aug 26, 2004 6:24 pm Post subject: Re: delete |
|
|
Christian Meier wrote:
| Quote: | Hi NG
I don't know if this problem is system dependend but I don't think so.
So, here is my assumption: "delete" deletes a variable. "delete[]"
deletes an array. Is this correct so far?
|
Not exactly (a variable is a named object, and dynamically allocated
objects don't have names - and of course you only delete dynamically
allocated objects), but I assume that by "a variable", you mean a
single object. So yes, it's correct.
| Quote: | And now check this code out:
#include
using namespace std;
int main()
{
char * p[100];
for(int i = 0; i < 100; ++i) {
p[i] = new char[200000];
} // for
for(int i = 0; i < 100; ++i) {
delete[] (p[i]);
//delete (p[i]); <-- this does the same on Linux with g++
} // for
}
p[i] is pointing to an array, so why doesn't it matter whether I write
"delete" with or without "[]"?
Or is this only on my system?
|
This might be system specific. Some systms will let you go without "[]",
but it's still not correct.
|
|
| Back to top |
|
 |
Ali Cehreli Guest
|
Posted: Thu Aug 26, 2004 7:21 pm Post subject: Re: delete |
|
|
On Thu, 26 Aug 2004 10:55:49 -0700, Christian Meier wrote:
| Quote: | So, here is my assumption: "delete" deletes a variable. "delete[]"
deletes an array. Is this correct so far?
|
Deleting includes calling destructors. Since fundamental types have
no-op destructors, you don't see any problem below.
| Quote: | for(int i = 0; i < 100; ++i) {
p[i] = new char[200000];
for(int i = 0; i < 100; ++i) {
delete[] (p[i]);
//delete (p[i]); <-- this does the same on Linux with g++
|
That is undefined behavior. Anything can happen... At least, the
destructors wouldn't be called if you used more complex types.
| Quote: | p[i] is pointing to an array, so why doesn't it matter whether I write
"delete" with or without "[]"?
Or is this only on my system?
|
Unfortunately it gives you the impression that it works, but it is
undefined behavior.
Ali
|
|
| Back to top |
|
 |
Karthiik Kumar Guest
|
Posted: Sat Aug 28, 2004 6:04 am Post subject: Re: delete |
|
|
Christian Meier wrote:
| Quote: | Hi NG
I don't know if this problem is system dependend but I don't think so.
So, here is my assumption: "delete" deletes a variable. "delete[]" deletes
an array. Is this correct so far?
And now check this code out:
#include
using namespace std;
int main()
{
char * p[100];
for(int i = 0; i < 100; ++i) {
p[i] = new char[200000];
} // for
for(int i = 0; i < 100; ++i) {
delete[] (p[i]);
//delete (p[i]); <-- this does the same on Linux with g++
} // for
}
p[i] is pointing to an array, so why doesn't it matter whether I write
"delete" with or without "[]"?
|
This can be clarified by having a much simpler example.
char * p = new char[200000];
// Do something with p
delete [] p;
| Quote: | Or is this only on my system?
|
This is undefined behaviour - meaning anything can happen.
Depending upon the implementation you are using, you can also think of
memory profilers to make sure that your program does not have any memory
leaks.
electricFence - on GNU/Linux just to name one.
mtrace utility on GNU/Linux .
These relevant utilities should help you understand if there is a
leak in your code.
--
Karthik.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|