Dimitri C.
Dimitri C.

Reputation: 22496

How to make data ownership explicit in C++

When working with pointers and references in C++, it is sometimes difficult to see whether the pointer has ownership over the referenced data, or if it is just a temporal reference. For example:

Instance* i = new Instance();
Instance* j = i;

How can it be made clear which of the 2 pointers has ownership over the instance? In other words, how to make clear on which pointer delete has to be called?

Note: In the above example this is not hard to see, as it is a very short piece of code. However, when the pointer is duplicated and passed around a lot, this can become unclear.

Upvotes: 15

Views: 1105

Answers (5)

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

As the others indicated - use a convention. I use raw pointers for non-owning variables, and the owner is usually wrapped into some kind of smart pointer (such as boost::scoped_ptr) or even not a pointer at all but an object created on the stack.

Upvotes: 2

Khaled Alshaya
Khaled Alshaya

Reputation: 96869

For me I would go with the Hungarian Notation!

Joel tells you the rest :: Making Wrong Code Look Wrong

an example in your case ::

Instance* Owener_i = new Instance();
Instance* Observer_j = i;
.
.
.
.
.
delete Observer_j; // Wrong! not an Owner.

Upvotes: 3

Michael Burr
Michael Burr

Reputation: 340208

You can use something like shared_ptr<> to explicitly share ownership. If you want to maintain a clear single owner with other non-owner pointers referring to the same object, you could use something like boost::scoped_ptr<> for the owning pointer and have a typedef for non-owning pointers:

typedef Instance* UnownedInstance_ptr;   // or some better name

This would at least document intent. I don't know of a way off the top of my head to have a smart pointer type that prevents the ability to delete the contained pointer and prevent copying the pointer into another smart pointer that takes ownership (since the source doesn't have any ownership to give away), but that might be an interesting class to represent that policy.

Upvotes: 3

ralphtheninja
ralphtheninja

Reputation: 133008

You cannot determine the owner, since there is no built in mechanism to know which pointer is owning the memory the pointer points to.

If you are really concerned about this, you could always introduce your own naming convention, e.g. through some pre/post-fix to your variable names. In other words, it's your code design that can give you this information. Since you (and your coworkers) are writing the code you can always make sure that this design is enforced during implementation. This of course means that everyone has to follow these "rules".

This is one reason why a common coding convention is so important. So you can read your own and other peoples code and understand it.

Upvotes: 7

Daniel Earwicker
Daniel Earwicker

Reputation: 116674

Firstly, it seems unnecessarily confounding to use a reference to refer to data that must be deleted. Use a pointer instead.

Secondly, if you want to indicate ownership of an object, use a wrapper class that manages ownership. There is auto_ptr specifically for this purpose, although it has shortcomings. (These should be addressed by unique_ptr in the next version of the language, though that doesn't help you now).

Thirdly, in the simplest cases (as often as possible), don't use the heap directly. Just declare a local object, e.g.

std::vector<int> v;

This doesn't stop you transfering ownership when you need to (use swap).

Upvotes: 7

Related Questions