private overloading of new & delete

Posted on 2005-04-26
Last Modified: 2010-04-01

I have been looking at some custom memory management and I am curious about new/delete overloading. I would like to know if one can limit overloaded new & delete operators to a template specific class (meaning all de-/allocations of all types occurring in the class are using the overloads). And also subject the overloads to the template for that class so different class variants use different overloads, while those overloads do not affect any code outside the given class. To give some more clarity se the example “code” below:

template <class Allocator>
class Dummy {
      // the operators should only be used by the class itself.
      // assume Allocator is a class supplied with the necessary functions

      void *operator new (size_t size) {
            return Allocator::allocSomeMemory(size); // call template specific function

      void operator delete (void *ptr) {
            Allocator::deallocTheMemory(ptr); // call template specific function


      void foobar() {
            int *x = new int; // calls the overloaded new operator.
            // please note that the allocated object could be of any type,
            // including classes

            delete x; // calls the overloaded delete operator

Unfortunately this code won’t compile and even if it would the overloads would only be called when de-/allocating the Dummy class, which is not desirable.
Question by:Aidman
    LVL 86

    Expert Comment

    Binding 'new' and 'delete' to a class IMHO does not really make sense anyway, since these operators are required to be accessible at 'global scope', i.e. 'outside' your class. What you can do is providing/defining operators that in return use your allocators.
    LVL 5

    Accepted Solution

    there are two way to customizing memroy management:
    1 overload global operator new, construct your objects with replacement new;
    2 using template, passing Allocator into a template class, allocating memory by Allocator's
    method, and using construct function to construct your own objects;
    Take a look at the source of STLport:
      _STLP_TRY {
          __new_finish = __uninitialized_copy(this->_M_start, __position, __new_start, __false_type());
          // handle insertion
          if (__fill_len == 1) {
            _Construct(__new_finish, __x);
          } else
            __new_finish = __uninitialized_fill_n(__new_finish, __fill_len, __x, __false_type());
          if (!__atend)
            // copy remainder
            __new_finish = __uninitialized_copy(__position, this->_M_finish, __new_finish, __false_type());
        _M_set(__new_start, __new_finish, __new_start + __len);
    LVL 8

    Expert Comment

    Correct me if I'm wrong, but class specific new and delete overloads are static members so it shouldn't affect what variation of that class you're using.

    LVL 9

    Expert Comment

    > class specific new and delete overloads are static members.
    That they are.

    > the overloads would only be called when de-/allocating the Dummy class
    Precisely, which is why it's not a good solution for you.

    > meaning all de-/allocations of all types occurring in the class are using the overloads
    These operator new/delete act on type, not scope. If scope is truely what you after then jkr's suggestion of providing allocation/deallocation routines other than operator new/delete is a tidy solution.

    It is possible to re-implement the global opertor new/delete pair but this is not what you want either, as *every* memory request would be handed by these.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now