I am developing a noSQL database and currently moving the code from C to C++.

It have class Pair, which contains a raw pointer to a memory blob.

class Pair {
  Blob* blob;

Then if you add this Pair to the List class, the List class stores this raw pointer internally, without copying. In order not to leak memory, the List notifies the Pair not to delete the raw pointer.

class Pair {
  Blob* _blob;
  bool _owns_blob;

  Pair(Blob* blob, bool owns_blob = true)
    : _blob(blob), _owns_blob(owns_blob) {}
  ~Pair() { if (_owns_blob) delete _blob; }
  Blob* blob() { return _blob; }
  void disown() { _owns_blob = false; }

void List::put(Pair& p) {

Because of this notification, I can not use a method signature like this:

List.put(const Pair &p)

and I will need to pass by value or by non-const reference.

Later if you request the data, the program creates a new Pair and includes the raw pointer inside, and also instructs the Pair not to delete the pointer:

Pair List::get(...) {
  return Pair(get_blob(), false);

Would this model benefit from shared pointers? How would this affect the speed and memory consumption?

  • 1
    I found your question fairly difficult to understand, so I tried to clean it up and illustrate your question with code examples. Please edit the question if I made a mistake or if I misunderstood your question.
    – amon
    Jun 28, 2015 at 9:41
  • 1
    Indeed, rvalue appears to be the right answer, because in this case, adding a Pair to a List causes the List to consume (take away) the ownership of the blob from the Pair. As a result the Pair does not own the blob anymore.
    – rwong
    Jun 28, 2015 at 10:15
  • @amon - excellent edit!!! I always seems to have problem to ask clear question. thanks for the help.
    – Nick
    Jun 28, 2015 at 10:48

2 Answers 2


I'm no C++-Expert, but in the last days I did some research around "smart" pointers, so maybe I can help you:

Shared pointers may help you in this situation, but they may be just a too easy way out of a situation where it is not too clear in the first place who the data really belongs to. Accessing a shared pointer is cheap, but copying it has a small penalty since there has to be some reference counting to be done in the background.

If you are programming in c++11, try redesign your classes (if possible) using unique_ptr with move semantics which come at no overhead.


Would this model benefit from shared pointers?

It looks like you hold the pointers and take ownership of them. It also seemsn that client code doesn't share ownership. At this point (heh punny!), you would benefit from implementing this in terms of a std::unique_ptr, and exposing either a custom safe wrapper around a pointer, or a Blob* const to client code.

How would this [using std::shared_ptr] affect the speed and memory consumption?

Regarding the speed, we cannot say, unless we know all your algorithms, and even then, we may not be able to say until you measure.

With shared pointers, the speed will be affected, but nobody can tell if this is signifficant until you measure (the change will decrease performance, but if your changes are not on the critical execution path, such decreases would be rendered insignificant by other performance bottlenecks).

Regarding memory consumption, a shared pointer has a foot print ~ three times as big as a raw pointer: - the data pointer itself - the reference count - the pointer to the internal object holding the two values above.

This will be signifficant if you store many many (many many many) pointers, and insignifficant otherwise.

  • 1
    While memory footprint of shared pointer is miniscule, memory barriers in its implementation significantly slow down its reading.
    – Basilevs
    Jun 29, 2015 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.