2

Consider a simple class:

class MyInt { public: MyInt(); MyInt(const char *num); }; 

I want to intergrate reference counting design pattern in to the class, which means i need to keep track of how much pointers point to an instance of this class. I need to implement it in this class only or create a different class and inherit it.

Given this example code i want to clear any allocated memory of the program:

int main() { MyInt*a = new MyInt("10"); a = new MyInt("20"); delete a; return 0; } 

My Tries

I tried operator oveloading of '=' and adding referenceCount member:

MyInt &MyInt::operator=(const MyInt* right) { MyInt*left = this; *this = right; left->referenceCount -= 1; if (left->referenceCount == 0) { delete (left); } return *this; } 

But this does not work because we assign pointer of the class to another pointer.

Also tried to override the new and delete operators but can't seem to make it work and keep track of the number of pointer to an instance.

As it seems i need to implement four things: copy constructor, operator new, operator delete and operator =.

How can i effectivly keep track of the pointers and clear unpointed memory automaticly?

10
  • 9
    Use std::shared_ptr. It has this built in. Commented Nov 9, 2018 at 14:03
  • 1
    If you want an intrusive pointer, look at boost. Even if you do not use boost, you can copy the pattern. boost.org/doc/libs/1_61_0/libs/smart_ptr/intrusive_ptr.html Commented Nov 9, 2018 at 14:07
  • Looks like you have very limited knowledge of the topic, and topic is too broad for SO. Just google for intrusive smart pointers. Commented Nov 9, 2018 at 14:07
  • @Fabio why use something not standard, when the standard already provides the functionality? Commented Nov 9, 2018 at 14:10
  • @gsamaras I need to implement on my own in this exercise. Commented Nov 9, 2018 at 14:21

2 Answers 2

2

std::shared_ptr does exactly this. From the ref:

Manages the storage of a pointer, providing a limited garbage-collection facility, possibly sharing that management with other objects. [...] Once all shared_ptr objects that share ownership over a pointer have released this ownership, the managed object is deleted.

so I suggest you use this instead.

Sign up to request clarification or add additional context in comments.

3 Comments

I need the example code to work exactly as it is. This adds wrapping to the code.
@DannyHambourg The language doesn't allow what you are trying to do without adding a layer of abstraction. It's not possible to overload the assignment operator for a raw pointer.
@DannyHambourg Even though your pointer is to a custom type you can't overload operators for it. All pointers are considered the same (over simplification) and all operations on them use built in operators that can't be overloaded.
1

a is a pointer, so assigning to a will not involve MyInt::opterator= in any way. There is no way to detect when a pointer to T is assigned to by overloading T's operators. To do this, you would need to design a class type that behaves like a pointer. Then you could properly track when the pointer might leak an object and properly delete it. Fortunately for you, the standard library already provides this class. It's std::shared_ptr. Here is your example modified to use std::shared_ptr :

#include <memory> struct InfInt { InfInt(const char *) {} }; int main() { auto a = std::make_shared<InfInt>("10"); a = std::make_shared<InfInt>("20"); // the previous `a` is deleted // The object pointed to by `a` is automatically deleted when // the last reference to it goes out of scope return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.