1

Is my assumption, that in following example, memory referenced by b will be deallocated once instance of A goes out of scope at end of func(), correct?

class A{
public:
   A() {
        b = std::shared_ptr<char>(new char[100] { 0 } );
   }
   char* b;
}

void func {
   A a;
}
3
  • I would double check that using shared_ptr with an array in that fashion is correct. Maybe it is, I'm not sure. Commented Oct 30, 2015 at 3:34
  • @NeilKirk, shared_ptr<T[]> has been proposed for C++17. I thought I saw somewhere that it was accepted, but I could be wrong. Commented Oct 30, 2015 at 3:51
  • @NeilKirk: According to this, definitely not safe. Commented Oct 30, 2015 at 4:01

2 Answers 2

3

No, not correct. b is of type char * and you assign to it a shared_ptr<char>. You should get a compilation error.

Furthermore, the constructor is private, another compilation error.

And how do you access b in func()? It is private in A.

Obviously your exercise is incomplete... so I just go from what you provided.

Also I suggest to use unique_ptr in case you can say it is a unique ownership (which it appears to be in my opinion).

This compiles:

#include <memory>
#include <iostream>

class A {
public:

    A() {
        std::cout << "A created with unique pointer" << std::endl;
        b = std::unique_ptr<char>(new char[100] {
            0
        });
    }

    ~A() {
        std::cout << "A destroyed" << std::endl;
    }

private:
    std::unique_ptr<char> b;
};

void func() {
    A a;
}

int main() {
    std::cout << "Call func()" << std::endl;
    func();
    std::cout << "func() called" << std::endl;
    return 0;
}

And at the end of func() A is destroyed and with it the unique_ptr.

However, ask yourself if you really need to use pointer? In your case an automatic variable should be just fine; it does the same (i.e. being destroyed when func() exits.

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

Comments

2

You're assigning to a char* member, so I'm pretty sure the shared_ptr is cleaned up as soon as the assignment completes (because it wasn't stored to another shared_ptr that would maintain the reference count).

If you want to use shared_ptrs, you have to use them consistently; a raw pointer class member can't manage anything for you. The pointer is still initialized to where the memory was allocated, but it's not allocated anymore; accessing it even a line later in the constructor would be undefined behavior/a use-after-free bug.

In any event, not safe because default shared_ptr uses delete, not delete[]. Is there some reason a std::shared_ptr<std::array<char, 100>> couldn't be used?

1 Comment

so assigning it to b doesn't increases shared_ptr's ref count? And why does std::shared_ptr<std::array<char, 100>> work correctly?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.