Question

In: Computer Science

I'm having trouble understanding smart pointer functions what is get(), release(), reset(), swap() for unique pointers?...

I'm having trouble understanding smart pointer functions

what is get(), release(), reset(), swap() for unique pointers? How do I use it?

what is get(), reset(), swap(), unique(), use_count() for shared pointers? How do I use it?

what is expired(), lock(), reset(),swap(), use_count for weak pointer? How do I use it?

Solutions

Expert Solution

Smart Pointer Functions:

A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way.

If you feel you need to use pointers, you would normally want to use a smart pointer as this can alleviate many of the problems with raw pointers, mainly forgetting to delete the object and leaking memory.

With raw pointers, the programmer has to explicitly destroy the object when it is no longer useful.

Smart pointers have pointer semantics, not value semantics (well, not the way you mean it). Think of shared_ptr<T> as a T*; treat it as such (well, except for the reference counting and automatic deletion). Copying a smart pointer does not copy the object it points to, just like copying a T* does not copy the T it points to.

You can't copy a unique_ptr at all. The whole point of the class is that it cannot be copied; if it could, then it wouldn't be a unique (ie: singular) pointer to an object. You have to either pass it by some form of reference or by moving it.

Smart pointers are all about ownership of what they point to. Who owns this memory and who will be responsible for deleting it. unique_ptr represents unique ownership: exactly one piece of code owns this memory. You can transfer ownership (via move), but in so doing, you lose ownership of the memory. shared_ptr represents shared ownership.

A) Unique Pointers:

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.

The following are it's pointer functions :

1) get() : Returns a pointer to the managed object or nullptr if no object is owned.

pointer get() const noexcept;

  Parameters : none

  Return value : Pointer to the managed object or nullptr if no object is owned.

Example

#include <iostream>
#include <string>
#include <memory>
 
int main(){
    std::unique_ptr<std::string> s_p(new std::string("Hello, world!"));
    std::string *s = s_p.get();
    std::cout << *s << '\n';
}

Output:

Hello, world!

2) release() : Releases ownership of its stored pointer, by returning its value and replacing it with a null pointer.

pointer release() noexcept;

Parameters : none

Return value : A pointer to the object managed by unique_ptr before the call.

Example:

#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> auto_pointer (new int);
  int * manual_pointer;

  *auto_pointer=10;

  manual_pointer = auto_pointer.release(); // (auto_pointer is now empty)

  std::cout << "manual_pointer points to " << *manual_pointer << '\n';

  delete manual_pointer;

  return 0;
}

Output:

manual_pointer points to 10

3) reset() : Destroys the object currently managed by the unique_ptr (if any) and takes ownership of p.

void reset (pointer p = pointer()) noexcept;

  Parameters : Pointer whose ownership is taken over by the object.

  Return value : none

Example :

#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> up;  // empty

  up.reset (new int);       // takes ownership of pointer
  *up=5;
  std::cout << *up << '\n';

  up.reset (new int);       // deletes managed object, acquires new pointer
  *up=10;
  std::cout << *up << '\n';

  up.reset();               // deletes managed object

  return 0;
}

Output:

5
10

4) swap() : Exchanges the contents of the unique_ptr object with those of x, transferring ownership of any managed object between them without destroying either.

  void swap (unique_ptr& x) noexcept;

  Parameters : Another unique_ptr object of the same type (i.e., with the same class template parameters).

  Return value : none

Example:

#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> foo (new int(10));
  std::unique_ptr<int> bar (new int(20));

  foo.swap(bar);

  std::cout << "foo: " << *foo << '\n';
  std::cout << "bar: " << *bar << '\n';

  return 0;
}

Output:

foo: 20
bar: 10

B) Shared Pointers

:std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.

The following are it's pointer functions :

1) get() ; Returns the stored pointer.

element_type* get() const noexcept

  Parameters : none

  Return value : The stored pointer.

Example :

#include <iostream>
#include <memory>
#include <string_view>
 
void output(std::string_view msg, int const* pInt){
    std::cout << msg << *pInt << "\n";
}
 
int main(){
    int* pInt = new int(42);
    std::shared_ptr<int> pShared = std::make_shared<int>(42);
 
    output("Naked pointer ", pInt);
    // output("Shared pointer ", pShared); // compiler error
    output("Shared pointer with get() ", pShared.get());
 
    delete pInt;
}

Output:

Naked pointer 42
Shared pointer with get() 42

2) reset() : For signature (1) the object becomes empty (as if default-constructed).

void reset() noexcept;

  Parameters : Pointer whose ownership is taken over by the object

Return Value : None

Example:

#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> sp;  // empty

  sp.reset (new int);       // takes ownership of pointer
  *sp=10;
  std::cout << *sp << '\n';

  sp.reset (new int);       // deletes managed object, acquires new pointer
  *sp=20;
  std::cout << *sp << '\n';

  sp.reset();               // deletes managed object

  return 0;
}

Output:

10
20

3) swap() : Exchanges the contents of the shared_ptr object with those of x, transferring ownership of any managed object between them without destroying or altering the use count of either.

void swap (shared_ptr& x) noexcept;

Parameters : Another shared_ptr object of the same type (i.e., with the same class template parameter T).

Return Value : None

Example:

#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> foo (new int(10));
  std::shared_ptr<int> bar (new int(20));

  foo.swap(bar);

  std::cout << "*foo: " << *foo << '\n';
  std::cout << "*bar: " << *bar << '\n';

  return 0;
}

Output::

*foo: 20
*bar: 10

4) unique() : Returns whether the shared_ptr object does not share ownership over its pointer with other shared_ptr objects (i.e., it is unique).

bool unique() const noexcept;

Parameters : None

Return Value : true if this is a unique shared_ptr, false otherwise

Example :

#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> foo;
  std::shared_ptr<int> bar (new int);

  std::cout << "foo unique?\n" << std::boolalpha;

  std::cout << "1: " << foo.unique() << '\n';  // false (empty)

  foo = bar;
  std::cout << "2: " << foo.unique() << '\n';  // false (shared with bar)

  bar = nullptr;
  std::cout << "3: " << foo.unique() << '\n';  // true

  return 0;
}

Output :

foo unique?
1: false
2: false
3: true

5) use_count() : Returns the number of different shared_ptr instances (this included) managing the current object. If there is no managed object, ​0​ is returned.

long use_count() const noexcept;

Parameters : None

Return Value : the number of shared_ptr instances managing the current object or ​0​ if there is no managed object.

Example :

#include <memory> 
#include <iostream> 
 
void fun(std::shared_ptr<int> sp){
    std::cout << "fun: sp.use_count() == " << sp.use_count() << '\n'; 
}
 
int main() { 
    auto sp1 = std::make_shared<int>(5);
    std::cout << "sp1.use_count() == " << sp1.use_count() << '\n'; 
 
    fun(sp1);
}

Output :

sp1.use_count() == 1
fun: sp.use_count() == 2

C) Weak Pointers :


std::weak_ptr models temporary ownership: when an object needs to be accessed only if it exists, and it may be deleted at any time by someone else, std::weak_ptr is used to track the object, and it is converted to std::shared_ptr to assume temporary ownership.

If the original std::shared_ptr is destroyed at this time, the object's lifetime is extended until the temporary std::shared_ptr is destroyed as well.std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr. It must be converted to std::shared_ptr in order to access the referenced object.

The following are its pointer functions:

1) expired() : Equivalent to use_count() == 0. The destructor for the managed object may not yet have been called, but this object's destruction is imminent (or may have already happened).

bool expired() const noexcept;

Parameters : None

Return Value : true if the managed object has already been deleted, false otherwise.

Example :

#include <iostream>
#include <memory>
 
std::weak_ptr<int> gw;
 
void f(){
    if (!gw.expired()) {
        std::cout << "gw is valid\n";
    }
    else {
        std::cout << "gw is expired\n";
    }
}
 
int main(){
{
        auto sp = std::make_shared<int>(42);
        gw = sp;
 
        f();
    }
 
    f();
}

Output:

gw is valid
gw is expired

2) lock() : Creates a new std::shared_ptr that shares ownership of the managed object. If there is no managed object, i.e. *this is empty, then the returned shared_ptr also is empty.

std::shared_ptr<T> lock() const noexcept;

Parameters : None

Return Value : A shared_ptr which shares ownership of the owned object if std::weak_ptr::expired returns false. Else returns default-constructed shared_ptr of type T.

Example :

#include <iostream>
#include <memory>
 
void observe(std::weak_ptr<int> weak) {
    if (auto observe = weak.lock()) {
        std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
    } else {
        std::cout << "\tobserve() unable to lock weak_ptr<>\n";
    }
}
 
int main(){
    std::weak_ptr<int> weak;
    std::cout << "weak_ptr<> not yet initialized\n";
    observe(weak);{
        auto shared = std::make_shared<int>(42);
        weak = shared;
        std::cout << "weak_ptr<> initialized with shared_ptr.\n";
        observe(weak);
    }
 
    std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
    observe(weak);
}

Output :

weak_ptr<> not yet initialized
        observe() unable to lock weak_ptr<>
weak_ptr<> initialized with shared_ptr.
        observe() able to lock weak_ptr<>, value=42
shared_ptr<> has been destructed due to scope exit.
        observe() unable to lock weak_ptr<>

3) reset() : Releases the reference to the managed object. After the call *this manages no object.

  The object becomes empty, as if default constructed.

void reset() noexcept;

Parameters : None

Return Value : None

Example :

#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> sp (new int(10));

  std::weak_ptr<int> wp(sp);

  std::cout << "1. wp " << (wp.expired()?"is":"is not") << " expired\n";

  wp.reset();

  std::cout << "2. wp " << (wp.expired()?"is":"is not") << " expired\n";

  return 0;
}

Output :

1. wp is not expired
2. wp is expired

4) swap() : Exchanges the contents of the weak_ptr object with those of x, swapping their owning groups and any stored data

  void swap (weak_ptr& x) noexcept;

Parameters : Another weak_ptr object of the same type (i.e., with the same class template parameter T).

Return Value : None

Example :

#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> sp1 (new int(10));
  std::shared_ptr<int> sp2 (new int(20));

  std::weak_ptr<int> wp1(sp1);
  std::weak_ptr<int> wp2(sp2);

  wp1.swap(wp2);

  std::cout << "sp1 -> " << *sp1 << '\n';
  std::cout << "sp2 -> " << *sp2 << '\n';
  std::cout << "wp1 -> " << *wp1.lock() << '\n';
  std::cout << "wp2 -> " << *wp2.lock() << '\n';

  return 0;
}

Output :

sp1 -> 10
sp2 -> 20
wp1 -> 20
wp2 -> 10

5) use_count() : Returns the number of shared_ptr instances that share ownership of the managed object, or ​0​ if the managed object has already been deleted, i.e. *this is empty.

long use_count() const noexcept;

Parameters : None

Return Value : The number of shared_ptr instances sharing the ownership of the managed object at the instant of the call.

//* I Really Hope this helps you Understand :) *//


Related Solutions

I'm having trouble printing a fibonacci sequence using pointers with malloc's to get this output. now...
I'm having trouble printing a fibonacci sequence using pointers with malloc's to get this output. now printing with pointers: 0 1 1 2 3 5 8 13 21 34 55 89 Here's my code: #include <stdio.h> #include <stdlib.h> void fib1(int a[]); void fib2(int* a); } int main() {   int arr[2] = {0,1};   int *pointer;   arr[0]=0;   arr[1]=1;   fib1(arr);      return 0; } void fib1(int a[]){   printf("printing with arrays:\n");        for(int i=0; i<6; i++){     printf("%d %d ", a[0],a[1]);     a[0] = a[0] +...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the...
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the story. Why would Rama be set apart as a true hero? The story of Rama and Sita is a favorite story that parents tell their children. What purpose does the Ramayana serve as an instructional story for Indian culture? What effect do the test and temptations have on the heroic character?
If anyone could simplify this for me. I'm having trouble understanding the material and I just...
If anyone could simplify this for me. I'm having trouble understanding the material and I just need a keep it simple stupid approach Discuss the various non influential as well as influential investments that company may have on their financial statements. Also compare and contrast how they are treated/recorded on the companies financial statements.
MICROBIOLOGY: I'm having trouble understanding the role of thermal death time and thermal death point in...
MICROBIOLOGY: I'm having trouble understanding the role of thermal death time and thermal death point in proper sterilization. Can someone please explain the role of both in proper sterilization?
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
hello, I'm having trouble understanding how to do these two problems could you show me a...
hello, I'm having trouble understanding how to do these two problems could you show me a step by step. 1)Eight sprinters have made it to the Olympic finals in the 100-meter race. In how many different ways can the gold, silver, and bronze medals be awarded? 2)Suppose that 34% of people own dogs. If you pick two people at random, what is the probability that they both own a dog? Give your answer as a decimal (to at least 3...
Directional and non-directional Hello! I'm having som trouble understanding the meaning of directional and non-directional hypothesis....
Directional and non-directional Hello! I'm having som trouble understanding the meaning of directional and non-directional hypothesis. What does it mean, and how do this affect the p-value? I also heard about "negative directional alternative" but could not find any information about this Thank you!
Hi, I'm having trouble understanding "symbolic microprogram" in Computer Organization and Architecture 2. Can someone please...
Hi, I'm having trouble understanding "symbolic microprogram" in Computer Organization and Architecture 2. Can someone please solve any example and explain it in details so I can understand it? Thank you in advance. NOTE: Please use Computer System Architecture (3rd edition)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT