Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

2992 questions
1
vote
1 answer

I have a question about std::vector

I have a doubt about the structure of std::vector If there are some class called foo. I will write down some code for explanation. class foo { //do something... }; void main(void) { foo a; std::vector> foo_list; //Is…
mimic
  • 59
  • 6
1
vote
0 answers

calling class method through shared ptr passes(without initialization) but accessing field causes segv(which is expected)

have a doubt regarding method calls from a class variable that points to a nullptr, I expected it to cause segv but it passed and SEGV was observed only when i tried accessing a class variable through th null ptr object. Wanted to know how does it…
1
vote
1 answer

C++ primer 5 edition: Container of shared_ptr

Again reading C++ Primer 5 Edition. I am on chapter 12 Dynamic memory. Everything is OK. Until this point in the book: "Because memory is not freed until the last shared_ptr goes away, it can be important to be sure that shared_ptrs don’t stay…
Itachi Uchiwa
  • 1,659
  • 6
  • 20
1
vote
2 answers

C++ primer 5 edition: count reference and underlying pointers

In C++ primer 5 Edition. Chapter 12 std::shared_ptr it is said that: p = q; "p and q are shared_ptrs holding pointers that can be converted to one another. Decrements p's reference count and increments q's count, deletes p's existing memory if p's…
Itachi Uchiwa
  • 1,659
  • 6
  • 20
1
vote
1 answer

Building an Object System Around shared_ptr

I am using shared_ptr as my garbage collection for a toy language that I am working on which compiles to C++. My objects derive from a common base class above that there are strings and numbers then there are vectors and maps. Everything on the c++…
Hamza Yerlikaya
  • 47,689
  • 37
  • 135
  • 231
1
vote
1 answer

Is using shared_ptr after reset undefined behaviour?

I have this test program: #include #include class A { public: A() { std::cout<<"A Constructor\n"; } ~A(){ std::cout<<"A Destructor\n"; } void show() { …
Zoso
  • 2,286
  • 1
  • 12
  • 22
1
vote
3 answers

Can smart pointers be implicitly used as pointers?

Are smart pointers considered as pointers? And thus can they implicitly used as pointers? Let's say I have the following class: class MyClass { //... std::shared_ptr foo() { /*whatever*/ }; void bar(AnotherClass* a) {…
Baptiste Merliot
  • 761
  • 8
  • 21
1
vote
1 answer

Why does the shared pointer does not delete memory?

shared_ptr pNico(new string("Nico")); shared_ptr pJutta(new string("Jutta")); // put them multiple times in a container vector>…
Espionage
  • 215
  • 3
  • 15
1
vote
2 answers

what if I ignore the return value of a function with shared_ptr return type

#include #include using namespace std; shared_ptr func() { shared_ptr ptr = make_shared("smart poiter"); return ptr; } int main(int argc, char const *argv[]) { func(); cout << "pause" <<…
陈培鸿
  • 31
  • 7
1
vote
1 answer

How to convert from std::shared_ptr to std::unique_ptr?

So i'm trying to build a template class for binary trees. I have a special way that i want to use to initialize the tree (which you can see in main()). The constructor works with shared_ptr just fine, but i want to move it to more lightweight…
1
vote
1 answer

std::shared_ptr constructor problems

The current C++ compilers fail to find a suitable overload for std::shared_ptr when using a C-array as a type. I can make it a real std::array object and that works, but the library I'm linking against (fftw3) has already created the typedef and…
Meta.x.gdb
  • 163
  • 1
  • 6
1
vote
1 answer

Bad_weak_ptr caused by call to shared_from_this with multiple inheritence

I am trying to understand why there is an bad_weak_ptr exception when calling shared_from_this. #include #include class parent : public std::enable_shared_from_this { public: void compare(std::shared_ptr
1
vote
1 answer

Casting shared_ptr in initializer list

Is it possible to up-cast a shared pointer object reference in an initializer list? I have two class hierarchies with matching derived classes. Let's say I have a Base class which is derived to a Foo and a Bar class. Then I have a separate…
cypheratheist
  • 153
  • 2
  • 9
1
vote
2 answers

How to prevent deletion of a raw pointer that is used as shared pointer?

I implemented a C-API for a C++ class which uses shared-pointers of other objects to access them. In my C-API I can of course only get raw pointers. So I "convert" the raw pointer in my C-API to a shared-pointer and use this then with my C++ class…
1
vote
2 answers

Call Operator() for custom class std::shared_ptr instance

I have my own extended 2d-array class CustomDynamicArray which cover std::vector and allow handle its items by indexes via overloaded operator() CustomCell& CustomDynamicArray::operator()(size_t colIdx, size_t rowIdx) until I had simple…
Malov Vladimir
  • 415
  • 4
  • 9
1 2 3
99
100