9

I think I have a considerable experience with normal (functional) designed patters, as described e.g. in the gang of four book, which I mainly used in java and C#. In these "managed" languages this is pretty much everything you need to know to get your work done.

However, in C++ world the developer also has the control of how all the objects get allocated, passed around and deleted. I understand the principles (I read Stroutrup among other texts), but it still takes me a lot of effort to decide which mechanism is best for a given scenario - and this is where a portfolio of memory-related design patterns would be useful.

For example, yesterday I had to create a class Results, that was a container for a few objects and a collection (std::vector in this case) of yet another type of objects. So there are a few design questions I couldn't really answer:

  1. Should I return this class by value, or by smart pointer?
  2. Inside the class, should the vector and the objects be normal members, or should they be stored as smart pointers again?
  3. In the vector, should I store the objects directly, or smart pointers to them again?
  4. What should the getters defined on my Results class return (i.e. values, references or smart pointers)?

Of course, smart pointers are cool and what not, but they create syntactic clutter and I am not convinced if using malloc for every single object is optimal approach.

I would be grateful for answers for the specific points above, but even more for some longer and more general texts on memory-related design patterns - so that I can solve the problems I will have on Mondays as well!

stdcall
  • 27,613
  • 18
  • 81
  • 125
Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • 2
    Have you heard of RAII? From your question, it doesn't seem clear enough if you're familiar wirh RAII, and you're talking about something beyond it. Is that the case? – Nawaz Jan 26 '13 at 17:38
  • Get [Effective C++ By Scott Meyers](http://www.amazon.co.uk/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889) There is a whole section in there on RAII and memory management etc. – drone.ah Jan 26 '13 at 17:41
  • @Nawaz, yes, I am familiar with RAII. I have Scotts Meyers' book as well. I don't think it helps in this case in any way though... – Grzenio Jan 26 '13 at 19:07

1 Answers1

27

The answer to all of your questions ends up being one and the same: it depends on whether you need reference semantics or value semantics (with some caveats to be taken into account).

If you need reference semantics, which is what you have by default in languages like Java and C# for UDTs (User-defined Data Types) declared with the class keyword, then you will have to go for smart pointers. In this scenario you want several clients to hold safe aliases to a specific object, where the word safe encapsulates these two requirements:

  1. Avoid dangling references, so that you won't try to access an object that doesn't exist anymore;
  2. Avoid objects which outlive all of the references to them, so that you won't leak memory.

This is what smart pointers do. If you need reference semantics (and if your algorithms are not such to make the overhead of reference counting significant where shared ownership is needed), then you should use smart pointers.

You do need reference semantics, for instance, when you want the same object to be part of several collections. When you update the object in one collection, you want the representations of the same object in all the other collections to be consistently updated. In this case, you store smart pointers to your objects in those collections. Smart pointers encapsulate the identity of an object rather than its value.

But if you do not need to create aliases, then value semantics is probably what you should rely on. This is what you get by default in C++ when you declare an object with automatic storage (i.e. on the stack).

One thing to consider is that STL collections store values, so if you have a vector<T>, then copies of T will be stored in your vector. Always supposing that you do not need reference semantics, this might become anyway an overhead if your objects are big and expensive to copy around.

To limit the likelyhood of this scenario, C++11 comes with move operations, which make it possible to efficiently transfer objects by value when the old copy of the object is no more needed.


I will now try to use the above concepts to answer your questions more directly.

1) Should I return this class by value, or by smart pointer?

It depends on whether you need reference semantics or not. What does the function do with that object? Is the object returned by that function supposed to be shared by many clients? If so, then by smart pointer. If not, is it possible to define an efficient move operation (this is almost always the case)? If so, then by value. If not, by smart pointer.

2) Inside the class, should the vector and the objects be normal members, or should they be stored as smart pointers again?

Most likely as normal members, since vectors are usually meant to be conceptually a part of your object, and their lifetime is therefore bound to the lifetime of the object that embeds them. You rarely want reference semantics in such a scenario, but if you do, then use smart pointers.

3) In the vector, should I store the objects directly, or smart pointers to them again?

Same answer as for point 1): do you need to share those objects? Are you supposed to store aliases to those objects? Do you want changes to those objects to be seen in different parts of your code which refer those objects? If so, then use shared pointers. If not, is it possible to efficiently copy and/or move those objects? If so (most of the time), store values. If not, store smart pointers.

4) What should the getters defined on my Results class return (i.e. values, references or smart pointers)?

Same answer as for point 2): it depends on what you plan to do with the returned objects: do you want them to be shared by many parts of your code? If so, return a smart pointer. If they shall be exclusively owned by just one part, return by value, unless moving/copying those objects is too expensive or not allowed at all (quite unlikely). In that case, return a smart pointer.


As a side note, please be aware that smart pointers in C++ are a bit trickier than Java/C# references: first of all, you have two main flavors of smart pointers depending on whether shared ownership (shared_ptr) or unique ownership (unique_ptr) is desired. Secondly, you need to avoid circular references of shared_ptr, which would create islands of objects that keep each other alive even though they are no more reachable by your running code. This is the reason why weak pointers (weak_ptr) exist.

These concept naturally lead to the concept of responsibility for managing the lifetime of an object or (more generally) the management of a used resource. You might want to read about the RAII idiom for instance (Resource Acquisition Is Initialization), and about exception handling in general (writing exception-safe code is one of the main reasons why these techniques exist).

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    C# has both: value-semantic UDT and reference-semantic UDT, defined with `struct` and `class` respectively. +1 Good answer anway. – Nawaz Jan 26 '13 at 18:36
  • Thanks for a very good answer! In my particular case I think I will store all these things as normal members. Do I need to do anything special with my classes to make them work with the move semantics? – Grzenio Jan 26 '13 at 20:15
  • @Grzenio: you need to define a *move constructor*. But if you are not familiar with these concepts, I would first read some tutorial (if not books) about move semantics and rvalue references in C++11. See for instance [this very good introduction](http://thbecker.net/articles/rvalue_references/section_01.html) or [this tutorial](http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html) – Andy Prowl Jan 26 '13 at 20:23
  • @Andy: Actually, defining a move constructor usually should not be necessary. The compiler-defaulted one, which simply calls move constructors for each subobject, is often adequate. – Ben Voigt Mar 08 '13 at 21:40
  • @Ben: Agree, I should have remembered about the Rule of Zero and be a bit more careful about giving advice on defining move constructors. Thank you for pointing out – Andy Prowl Mar 08 '13 at 21:47