Question

We want to overload placement new operator just to verify that used memory size is enough for the given class. We know this size. The construction is more or less in this way:

template <size_t MAXSIZE>
class PlacementNewTest {
public:
  void* operator new (size_t size, void* where)
  {
     if (size > MAXSIZE) {
        throw bad_alloc();
     }
     return where;
  }
};

Let say used in such simplified context:

char buffer[200];

class A : public PlacementNewTest<sizeof buffer> {
public:
   char a[100];  
};

class B : public A {
public:
   char b[200];  
};


int main() {
   A* a = new (buffer) A; // OK
   a->~A();
   B* b = new (buffer) B; // throwed bad_alloc
   b->~B();
}

During testing phase I have this PlacementNewTest<> class used, but in the release code I consider to remove it. Do you think, basing on your experience, how much this will cost our performance, not to remove this extra test class? Is this only cost of this verification if (size > MAXSIZE)? In other words, what is performance penalty for such redefinition:

class PlacementNewNOP {
public:
  void* operator new (size_t size, void* where)
  {
     return where;
  }
};

Maybe it is not important in this question - but: This is, and must be, C++03. We cannot upgrade to C++11. And boost is not an option too, just C++03.

Was it helpful?

Solution

There shouldn't be any overhead apart from the comparison, unless you are using virtual methods, the binding is static.

Of course there is the exception overhead, but since that is something that shouldn't happen, you should be safe to ignore it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top