?
Solved

private overloading of new & delete

Posted on 2005-04-26
4
Medium Priority
?
221 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:Aidman
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13872424
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
 
LVL 5

Accepted Solution

by:
rendaduiyan earned 750 total points
ID: 13874951
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
 
LVL 8

Expert Comment

by:_corey_
ID: 13879910
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
 
LVL 9

Expert Comment

by:_ys_
ID: 13884185
> 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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

862 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