delete []

This is a discussion on delete [] within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I remembered delete[] is implemented through operator overloading, but I am not quite clear. Could anyone recommend some ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    delete []

    Hello everyone,


    I remembered delete[] is implemented through operator overloading, but I am not quite clear.

    Could anyone recommend some links about how delete[] is implemented so that I can learn and refresh my memory? :-)


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure this isn't "correct". delete [] is a compiler defined function, just like delete "without []".

    Put another way:
    operator delete()
    and
    operator delete[]()
    are distinct different operator overloads.

    Sure, the internal mechanisms are (near enough) identical for the typical case, but they are still different functions in the compiler itself. They are not constructed in some header file by some magical template or operator overload.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The new, new[], delete and delete[] operators consist of two parts: memory management and lifetime management. The two forms of new allocate memory, then initialize the objects. The two forms of delete destruct the objects and then free the memory.

    It's the memory management part that you can overload.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,830
    To understand what happens with operator delete, it is necessary to understand what happens with operator new. I'll describe that first....

    The operation "p = new Thing[n];" does the following;
    1) if Thing has supplied an operator new[](), then p will be assigned the return value from Thing::operator new[](n). Otherwise, p will be assigned the return value from ::operator new[](n);
    2) Thing's default constructor will be invoked for each element of the array (n times).

    The behaviour of "delete [] p;" is exactly the reverse.
    1) Invoke destructors (~Thing())for each element of the array (n times), in reverse of the order that constructors where invoked when the array was created.
    2) if Thing has supplied a suitable operator delete [], then invoke Thing::operator delete [](p); otherwise, invoke ::operator delete[](p).

    The global operator new[] and global operator delete [] are supplied by the implementation (compiler/library), but can be redefined by the program, and are functions of the form;
    Code:
       void *::operator new(std::size_t n) throw(std::bad_alloc);
       void ::operator delete [](void *) throw();
    Individual classes can also supply their own operator new [] and delete [], of the form;
    Code:
    class Thing
    {
        public:
    
            void *operator new[](std::size_t) throw(std::bad_alloc);
            void operator delete [] (void *) throw();
    };
    There are a number of other variants, as there are multiple forms of new and delete (single element, array, and placement).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    1) if Thing has supplied an operator new[](),
    But note that inherited operators new functions are included in that, i.e.
    Code:
    class A
    {
    public:
       void *operator new[](std::size_t sz) throw(std::bad_alloc) { ... }
       void operator delete[](void *) throw() { ... }
    };
    
    class B : public A
    {
    };
    
    
    
    B *p = new B[100]; // A::operator new[] is used.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. Which comes first...delete [] or NULL?
    By Waldo2k2 in forum C++ Programming
    Replies: 13
    Last Post: 08-09-2002, 09:05 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21