-1

Duplicate as shared_ptr to an array : should it be used?


According to this post, the good way to wrap a array with smart_ptr is to define a deleter function and pass the deleter function to smart_ptr alone with raw arrays.


I'm going to refactor my code, like wrap raw arrays with smart_ptr. Here's a example:

Original codes:

    class MyList{
    public:
       MyList(int size = 0);
       void Resize(int size);
       ~MyList();
    private:
       int* myArray; 
       int* head_;
       size_t size_;
    }

    MyList::MyList(int size){
        myArray = new int[size]; //allocated memory when object is being created 
        head_ = list_;
        size_ = size;
    }

    void MyList::Resize(int size) {
        if (!list_) {
             delete myArray;
             size_ = 0;
        }
        myArray = new int[size];
        head_ = list_;
        size_ = size;
    }

    MyList::~MyList(){
       delete myArray;
       head = null_ptr;
    }  

My question is: How to correctly wrap raw arrays with smart_ptr?

4
  • try myArray = new myArraySize() Commented Jun 3, 2014 at 22:09
  • I didn't get it, can you give me more details? Thank you Commented Jun 3, 2014 at 22:11
  • 1
    you have to build a new instance with the new operator, which means calling the constructor, which means myArraySize(), not myArraySize. Of course I'm assuming that myArraySize is a well defined class type. Commented Jun 3, 2014 at 22:13
  • stackoverflow.com/questions/13061979/… Commented Jun 3, 2014 at 22:19

1 Answer 1

6

Don't use shared_ptr as your default smart pointer.

Thats unique_ptr. shared_ptr corresponds to shared ownership. You want unique ownership.

More information here.


Reassignment of smart pointers

Smart pointers are not assignable to raw pointers. That has good reasons.

Instead, write

myArray = std::make_shared<int>( myArraySize );
// Creates ONE int-object, which is initialized to myArraySize

Or use the initialization list directly:

myClass::myClass(int myArraySize) :
    myArray{ std::make_shared<int>(myArraySize) } {}

But you want an array!

Firstly, you should use std::size_t for specifying array bounds.

Now to the actual code for setting up the right smart pointer and initializing it:

std::unique_ptr<int[]> myArray;

// [...]

myClass::myClass(int myArraySize) :
    myArray{ new int[myArraySize]() } {}
    // value-initialized it

Or since C++1y (or with an own definition) with make_unique:

myClass::myClass(int myArraySize) :
    myArray{ std::make_unique<int[]>(myArraySize) } {}
Sign up to request clarification or add additional context in comments.

8 Comments

my bad. Still, using make_shared is at least more efficient, even if not more correct...
@Deduplicator How is make_shared more efficient? It merely wrapps the call.
@CharlesChow Did you even read the rest of my post!?
Assignment of a raw pointer to a shared pointer without giving a referenced other shared_ptr allocates a new reference-count+deleter object. make_shared combines this allocation with the payload allocation.
Here's what I did, std::unique_ptr<int[]> myArray; myArray.reset(new int[10]); and it's working, I just re assigned size 10 to myArray, did I do something bad?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.