private overloading of new & delete

Hi

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 {
private:
      
      // 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
      }

public:

      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.
AidmanAsked:
Who is Participating?
 
rendaduiyanConnect With a Mentor Commented:
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);
        ++__new_finish;
      } 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());
    }
    _STLP_UNWIND((_Destroy(__new_start,__new_finish),
                  this->_M_end_of_storage.deallocate(__new_start,__len)));
    _M_clear();
    _M_set(__new_start, __new_finish, __new_start + __len);
  }
0
 
jkrCommented:
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.
0
 
_corey_Commented:
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.

corey
0
 
_ys_Commented:
> 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.