diff between user defined delete and delete[]

Discussion in 'C++' started by mail.dsp@gmail.com, Nov 17, 2008.

  1. Guest

    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
    --
    Daya
     
    , Nov 17, 2008
    #1
    1. Advertising

  2. Salt_Peter Guest

    On Nov 17, 4:13 am, wrote:
    > 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)
     
    Salt_Peter, Nov 17, 2008
    #2
    1. Advertising

  3. Sachin Guest

    On Nov 17, 2:44 pm, Salt_Peter <> wrote:
    > On Nov 17, 4:13 am, wrote:
    >
    >
    >
    >
    >
    > > 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)- Hide quoted text -
    >
    > - Show quoted text -


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

    Thx,
    Sachin
     
    Sachin, Nov 17, 2008
    #3
  4. James Kanze Guest

    On Nov 17, 10:13 am, wrote:
    > 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???


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 17, 2008
    #4
  5. James Kanze Guest

    On Nov 17, 10:44 am, Salt_Peter <> wrote:
    > On Nov 17, 4:13 am, 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).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 17, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    513
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    642
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,930
    Steve Fulton
    Aug 31, 2004
  4. Santa
    Replies:
    1
    Views:
    1,087
    Mark A. Odell
    Jul 17, 2003
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    195
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page