delete

C

Christian Meier

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 = new char[200000];
} // for

for(int i = 0; i < 100; ++i) {
delete[] (p);
//delete (p); <-- this does the same on Linux with g++
} // for

}


p 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
 
R

Rolf Magnus

Christian said:
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.
And now check this code out:

#include <iostream>
using namespace std;

int main()
{
char * p[100];
for(int i = 0; i < 100; ++i) {
p = new char[200000];
} // for

for(int i = 0; i < 100; ++i) {
delete[] (p);
//delete (p); <-- this does the same on Linux with g++
} // for

}


p 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.
 
A

Ali Cehreli

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

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

Karthiik Kumar

Christian said:
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 = new char[200000];
} // for

for(int i = 0; i < 100; ++i) {
delete[] (p);
//delete (p); <-- this does the same on Linux with g++
} // for

}


p 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;
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top