Smart Pointers Flashcards

1
Q

Smart Pointers

What does RAII stand for?

A

Resource Acquisition is Initialization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the other two Acronyms RAII can be referred to as?

A
  • CADRe (CTORs Acquire DTORs Release)
  • SBRM (Scope Based Resource Management)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens when a class falls out of scope?

A

Its DTOR is called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would prevent a stack variable’s DTOR from being called?

A

An unchecked error (uncaught throw or segfault)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the first concept behind RAII?

A

That resource acquisition should be done during intialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the importance Acquiring during Initialization?

A

Moving past this point confirms that all data was initizialized correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the second concept behind RAII?

A

That resources must be released when data is deallocated. (delete pointers, close files, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Smart Pointers?

A

Objects that act as pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What library contains smart pointers?

A

<memory>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the benefits of using smart pointers?

A
  • Allow for CTORS and DTORS
  • They can be put on the stack and passed around more easily
  • Methods for working with data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 3 types of Smart Pointers?

A
  • Unique
  • Shared
  • Weak
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Unique Pointer?

A

A smart pointer that exclusively owns one object. It handles allocation, and deallocation of the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you syntactically declare a unique pointer?

A

unique_ptr<type> ptrName(new type);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What must the objects held by unique pointers have?

A

A DTOR. The unique_ptr will try to call this when deallocation occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When must you have a virtual DTOR?

A

When you have a Data Structure containing smart pointers to an inheritable object. Ensures DTORs can be called polymorphically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you copy data from one unique pointer to another?

A

Using the move() CTOR

17
Q

What is a Shared Pointer?

A

A smart pointer that allows multiple shared pointers to lay claim to an object. The object will only deallocate when there are no remaining pointers.

18
Q

How do you syntactically declare a shared pointer?

A

shared_ptr<type> ptrName(new Type);

19
Q

What is the benefit of shared pointers?

A

Allows copies of the pointer to be passed around referring to the same object without reinitializing.

20
Q

What is a weak pointer?

A

A smart pointer that can look at a shared pointer’s object without claiming ownership; i.e. it cannot prevent the object falling out of scope, it can just look.

21
Q

What are the two main use cases for weak pointers?

A
  • Looking into cached data
  • Preventing Circular dependencies (Ex: doubly linked list, front pointer is shared, back pointer is weak)