diff between user defined delete and delete[]

M

mail.dsp

Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]

class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}

void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}
};

If we perform
Test *p=0; delete p;
It calls operator delete.

But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].

Why???

I've executed this code on g++ 4.1.2 20070925.

Thanks in advance
 
S

Salt_Peter

Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]

class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}

void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}

};

If we perform
Test *p=0; delete p;
It calls operator delete.

But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].

Why???

The FAQ covers the subject. Read the 2 common techniques used with
primitive arrays

[16.14] After p = new Fred[n], how does the compiler know there are n
objects to be destructed during delete[] p?
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14

Although in modern code unless there is a really good reason:
a) don't allocate on the heap manually, use smart pointers or RAII
b) prefer dynamic containers over primitive, fixed arrays
(std::vector, std::deque, etc)
 
S

Sachin

Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]
class Test{
public:
        void * operator new (size_t t){
                cout<<"\nCalling... new";
                return malloc(t);
        }
        void operator delete (void *p){
                cout<<"\nCalling ... delete";
                free(p);
        }
        void * operator new [] (size_t t){
                cout<<"\nCalling ... new[]";
                return malloc(t);
        }
        void operator delete [] (void *p){
                cout<<"\nCalling ... delete[]";
                free(p);
        }

If we perform
Test *p=0;     delete p;
It calls operator delete.
But if we perform
Test *p=0;  delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0;   p=new Test[10];   delete[] p; It calls operator delete[].

The FAQ covers the subject. Read the 2 common techniques used with
primitive arrays

[16.14] After p = new Fred[n], how does the compiler know there are n
objects to be destructed during delete[] p?http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14

Although in modern code unless there is a really good reason:
a) don't allocate on the heap manually, use smart pointers or RAII
b) prefer dynamic containers over primitive, fixed arrays
(std::vector, std::deque, etc)- Hide quoted text -

- Show quoted text -

VC++ Compiler does call delete[], even if you dont call new[]
explicitly.

Thx,
Sachin
 
J

James Kanze

Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]
class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}

void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}
};
If we perform
Test *p=0; delete p;
It calls operator delete.
But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't
call operator new[].

I'm not sure how to read the above. Too many negations. But in
a delete expression, if the pointer is null, it is unspecified
whether the compiler calls the operator delete function or not.
Probably, your compiler calls it in the simple cases, but
doesn't bother calling it if it otherwise has to check for a
null pointer (usually the case with delete[]).
Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[]..

Why what? If the pointer isn't null, the compiler is required
to call operator delete. If the pointer is null, it is
unspecified whether operator delete is called or not (so the
operator delete function had better check). And it can
sometimes do one, sometimes the other---adding a virtual
destructor (or even any non-trivial destructor) might change
the behavior, for example.
 
J

James Kanze

On Nov 17, 4:13 am, (e-mail address removed) wrote:

[...]
If we perform
Test *p=0; delete p;
It calls operator delete.
But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].
Why???
The FAQ covers the subject. Read the 2 common techniques used
with primitive arrays

I don't think the FAQ has anything which addresses his question;
I couldn't find anything, anyway.
[16.14] After p = new Fred[n], how does the compiler know there are n
objects to be destructed during delete[] p?http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14

Which doesn't say anything about what happens when you pass a
null pointer to delete[] (and why what happens is, or may be,
different than what happens when you pass a null pointer to a
non-array delete). (Given that this is the first time I've seen
this question asked, it's not too surprising that it isn't in
the *Frequently* Asked Questions.
Although in modern code unless there is a really good reason:
a) don't allocate on the heap manually, use smart pointers or RAII

I'm not following you here. None of the smart pointers I've
seen take care of allocation; despite the name, nor does the
RAII idiom.
b) prefer dynamic containers over primitive, fixed arrays
(std::vector, std::deque, etc)

Agreed. And more generally, don't use dynamic allocation
(except that hidden within such containers) unless you need it
(in which case, smart pointers and RAII probably won't help).
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top