8

I have a question about how to limit the creation of object on heap or stack? For example, how to make sure an object not living on heap? how to make sure an object not living on stack?

Thanks!

skydoor
  • 25,218
  • 52
  • 147
  • 201
  • see http://stackoverflow.com/questions/1820069/public-operator-new-private-operator-delete-getting-c2248-can-not-access-priva – Martin Beckett Mar 03 '10 at 18:16

4 Answers4

18

To prevent accidental creation of an object on the heap, give it private operators new. For example:

class X {
private:
    void *operator new(size_t);
    void *operator new[](size_t);
};

To prevent accidental creation on the stack, make all constructors private, and/or make the destructor private, and provide friend or static functions that perform the same functionality. For example, here's one that does both:

class X {
public:
    static X *New() {return new X;}
    static X *New(int i) {return new X(i);}
    void Delete(X *x) {delete x;}
private:
    X();
    X(int i);
    ~X();
};
  • Nice one; of course, it can be circumvented by the naughty, using inheritance. – xtofl Nov 15 '10 at 11:13
  • I don't think you can inherit from `X`, because of the private destructor. You're right in general though, and I'm not sure how you'd work around it (however my experience has been that usually you want to inhibit creation on heap for a value object that isn't suitable as a base class anyway). –  Dec 14 '10 at 17:37
  • Srry, If I m wrong. But even if we want to prevent object creation on heap,Can't we still accomplish it without making "new" private? Still make constructor private and return the object through some static method.Is it right to make objects on stack with this approach? – Manish Nov 14 '13 at 04:51
  • 1
    ---1: `std::vector v(1);` Wow, that was easy. – Lightness Races in Orbit Nov 30 '14 at 05:58
  • @LightnessRacesinOrbit I don't get your comment, it doesn't work in g++4.9, as the object is not copy-able. – vsoftco Dec 14 '14 at 02:33
5

To prevent an object from being allocated on the stack define a private destructor. This results in a compilation error for a stack based object, as it prohibits the implicit destructor call when a stack based object goes out of scope. You will need to implement a public destroy method, something along the lines of:

void MyObject::destroy() const
{
   delete this;
}

to clean up your object that is allocated on the heap. Make sure to read the C++ FAQ for some caveats.

Similarly, to prevent an object from being allocated on the heap, make sure to define the new operator private.

Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
3

You can prevent an object being declared with automatic duration ("on the stack") by making its constructor private and providing a factory (abstract factory or factory method) to control creation of new objects.

You could try to prevent an object being allocated dynamically ("on the heap") by making its operator new private. However this doesn't prevent someone from creating an instance of your class using placement new.

Why would you want to control how your object is allocated? What problem are you trying to solve?

Philip Potter
  • 8,975
  • 2
  • 37
  • 47
0

It's easier to ensure the object is created on the heap. The new operator always does that. It is more difficult to ensure that the object is on the stack. For small objects, creating an object like MyType aObj; always uses the stack. For objects whose classes occupy huge amounts of memory, the compiler might not use the stack.

Kerido
  • 2,930
  • 2
  • 21
  • 34