Questions tagged [smart-pointer]

For questions about the usage of the smart pointer construct.

A smart pointer is an abstraction on a pointer allowing for such features as automatic memory management and bounds checking, while preserving the ability to act like a regular pointer.

34 questions
86
votes
11 answers

Why Garbage Collection if smart pointers are there

These days, so many languages are garbage collected. It is even available for C++ by third parties. But C++ has RAII and smart pointers. So what's the point of using garbage collection? Is it doing something extra? And in other languages like C#, if…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
70
votes
9 answers

std::shared_ptr as a last resort?

I was just watching the "Going Native 2012" streams and I noticed the discussion about std::shared_ptr. I was a bit surprised to hear Bjarne's somewhat negative view on std::shared_ptr and his comment that it should be used as a "last resort" when…
ronag
  • 1,179
  • 1
  • 10
  • 18
36
votes
1 answer

raw, weak_ptr, unique_ptr, shared_ptr etc... How to choose them wisely?

There is a lot of pointers in C++ but to be honest in 5 years or so in C++ programming (specifically with the Qt Framework) I only use the old raw pointer: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); I know there are a lot of…
CheshireChild
  • 499
  • 1
  • 5
  • 7
33
votes
5 answers

Why can't Java/C# implement RAII?

Question: Why can't Java/C# implement RAII? Clarification: I am aware the garbage collector is not deterministic. So with the current language features it is not possible for an object's Dispose() method to be called automatically on scope exit. But…
mike30
  • 2,788
  • 2
  • 16
  • 19
27
votes
5 answers

C++: Should class own or observe its dependencies?

Say I have a class Foobar that uses (depends on) class Widget. In good ol' days, Widget wolud be declared as a field in Foobar, or maybe as a smart pointer if polymorphic behavior was needed, and it would be initialized in constructor: class Foobar…
18
votes
4 answers

Will destructing a large list overflow my stack?

Consider the following singly linked list implementation: struct node { std::unique_ptr next; ComplicatedDestructorClass data; } Now, suppose I stop using some std::unique_ptr head instance that then goes out of scope, causing…
VF1
  • 1,891
  • 2
  • 14
  • 25
12
votes
4 answers

Using vectors of shared pointers to objects in my C++ code to prevent object duplication

In my C++ project, I have three classes, Particle, Contact, and Network. The Network class will have N particles (std::vector particles) and Nc contacts (std::vector contacts). Particle objects will each have a number of contacts,…
AnInquiringMind
  • 371
  • 1
  • 3
  • 9
9
votes
1 answer

Key / Value store development porting to modern C++

I am developing a database server similar to Cassandra. Development were started in C, but things became very complicated without classes. Currently I ported everything in C++11, but I am still learning "modern" C++ and have doubts about lot of…
Nick
  • 295
  • 2
  • 9
9
votes
3 answers

Polymorphic template container: shared_ptr vs reference_wrapper

Assuming we have two classes: class A { ... } class B : public A { ... } Would it be better to write std::deque > container; or std::deque > container; to create a container which is able to contain…
user108376
9
votes
4 answers

Is Non-Deterministic Resource-Management a Leaky Abstraction?

From what I can see, there are two pervasive forms of resource-management: deterministic destruction and explicit. Examples of the former would be C++ destructors and smart pointers or Perl's DESTROY sub, whilst an example of the latter would be…
Louis Jackman
  • 243
  • 1
  • 5
8
votes
2 answers

Should I use a unique_ptr with an array type, or a vector?

I've been out of C++ for years, last time I used it was back in gamedesign before C++11. I see all these new pointer types which seem great. But I'm unsure when and how to use them. In the old days I would create a buffer like this: uint…
Kevin
  • 844
  • 1
  • 7
  • 14
6
votes
2 answers

Smart Pointers inside class vs Normal Pointers with Destructor

Regarding pointers which are members of classes. Should they be of a smart pointer type or is it enough to simply deal with them in the destructor of the class they are contained in?
6
votes
1 answer

Procedure for migrating a large C++ code base to use smart pointers

I am in the process of refactoring a large C++ code (~2300 files, ~600K lines, mostly older C/C++98 style code) and there are definitely memory leaks that could be shored up using C++ smart pointers. Is there an incremental path towards migrating…
wcochran
  • 171
  • 5
6
votes
4 answers

Refactor big C++ application to prevent from bad pointer

In application which has about 1.5 mln lines of code there is using many pointers as a class members and in other places in code. Classes are usually very huge. It is possible to change to make it safer in quite fast way? Refactor it can take years.…
marcin
  • 79
  • 4
5
votes
1 answer

Why do we need to specify the type of data a pointer will hold, if all pointers are the same

Why do we need to specify the type of the data whose address, a pointer will hold, if all pointers are the same. Since all pointers store addresses. Also, the amount of space a pointer will require in memory depends on whether the machine is 32-bit…
amd
  • 59
  • 1
  • 3
1
2 3