Javatpoint Logo
Javatpoint Logo

Weak Pointer in C++

In this article, we will discuss the weak pointer in C++ with their syntax and examples.

The C++ weak_ptr is a standard library component. It is used to store weak references to any objects controlled by the shared_ptr standard library pointer, which is used to ultimately transform weak_ptr to shared_ptr. The shared_ptr that was ultimately created from the weak_ptr is used to access the referenced object. A weak_ptr loses its capacity to remain forever after it is transformed into a shared_ptr, indicating that it only exists momentarily. It is not advised to access weak_ptr first before granting access to the reference object.

Syntax:

It has the following syntax:

The type controlled by the weak pointer is used for the parameter supplied as Class T according to the syntax for the C++ weak_ptr function.

How does C++'s weak_ptr operate?

Every pointer in a high-level language is primarily used to reference objects and subsequently access elements that are available in an array in a well-organized style. Similar circumstances apply to C++'s weak_ptr. The weak_ptr working flow is described throughout the class template:

  • The weak_ptr used for referencing an object initially is not the actual pointer, as it is considered as a temporary pointer in terms of object referencing and assigning.
  • However, once weak_ptr gains final control or authority over share_ptr, object accessing and referencing become simple and efficient.
  • The main purpose of this coordination and sharing, and switching between the weak_ptr and share_ptr for reading and referencing, is to prevent lengthy cycles within the data structure.
  • When share_ptr assumes full responsibility for managing resources during resource allocation and analysis, weak_ptr loses all control and is erased.
  • A weak_ptr never allows access to the elements directly; instead, it uses the shared_ptr object, which owns the complete code, to call the member function Lock to access the code that needs to use the resource.
  • Once the Lock or weak_ptr is called for resource allocation or to control any bLock, an empty weak_ptr object is generated.
  • Once the Lock or the weak_ptr is jointly referenced or held by the majority of the shared_ptr object, one complete cycle has been accomplished or finished.
  • Instead of merely employing weak_ptr, which is a component of the complete working format of pointers in any of the pointer concepts, it makes use of all resources prominently with the aid of share_ptr.
  • The shared_ptr makes use of the weak_ptr's behaviour of obtaining Locks and weak_ptr's points to make the end resources freed and released by the weak_ptr.
  • The list and its related nodes will also be easily deleted and distinguished once all the aforementioned resources have been released, creating an optimized method of analysis and format sizing.
  • This complete cycle consists of numerous checks and methods, including the expired() method, which is used to determine whether or not ownership has expired.
  • By using Lock(), you can take an individual, exclusive Lock over a resource, preventing other resources from trying to target it and causing conflicts in their attempts to do so.
  • The weak_ptr pointing is checked using the owner () before, which returns true if it points correctly.
  • Within that entire cycle, the owned resource is released using Reset().
  • A swap() technique is used when two weak_ptr are acting on the objects.
  • The number of shared_ptr objects is counted and tracked using the use_count()
  • The technique for swapping out the initially owned resources is operator=.

Examples of weak_ptr in C++:

Here are some examples of weak_ptr in C++:

Example - 1:

Output:

Weak pointer value: 42
Weak pointer is expired after reset

Explanation:

In this example, the shared pointer shared_Ptr points to an int with the value 42. After that, a sharedPtr and a std::weak_ptrweakPtr are created. If the original object has been removed, the Lock() function returns an empty shared pointer, which is used to retrieve a shared pointer from a weak pointer.

The program prints the shared object's value first when it uses the weak pointer to access it. After that, the shared pointer is reset, simulating a scenario in which the initial shared pointer no longer controls the object. After resetting the shared pointer, the program tries to access the object again using the weak pointer.

Example - 2:

Output:

Acquired weak_ptr through Lock(): 14
Weak pointer expired: false
Weak pointer expired after reset: true

Example - 3:

Output:

Count of sharedPtr1: 1
Count of weakPtr: 1
Count of sharedPtr1 after sharedPtr2 creation: 2
Count of weakPtr after sharedPtr2 creation: 2

Example - 4:

Output:

*shrd_ptr_1 == 8
*shrd_ptr_1 == 10
*shrd_ptr_1 == 8

*wk_ptr_1 == 8
*wk_ptr_2 == 10
*wk_ptr_1 == 8

Explanation:

This program shows how to utilize the swap() method to swap the weak_ptr when it's necessary to get the needed resource, as seen by the output.

In the shared_ptr instances, the shrd_ptr_1 and shrd_ptr_2 that point to the integers 8 and 10 are created at the beginning of the supplied program. After that, the value of shrd_ptr_1 (which is 8) is printed. Next, it uses both the member function swap and the swap function from the standard library to swap the contents of shrd_ptr_1 and shrd_ptr_2. As a result, shrd_ptr_1's value shifts from 8 to 10 and back again. As part of the shared_ptr instances, the program creates two weak_ptr instances, wk_ptr_1 and wk_ptr_2. wk_ptr_1's value, 8, is printed by t. After that, the ownership of wk_ptr_1 and wk_ptr_2 are switched using the member function swap and the swap function from the standard library. After the values of wk_ptr_1 and wk_ptr_2 are switched, wk_ptr_1 now contains the number 10. The program ends by switching wk_ptr_1 and wk_ptr_2 once more to return to the previous ownership. After that, the value of wk_ptr_1 is printed, which returns 8.

Conclusion:

The C++ weak_ptr function is essential for obtaining and accessing the list node's elements. Additionally, shared_ptr and weak_ptr work together to create an optimized cycle for accessing the elements. Once shared_ptr has been chosen, it is generally thought of as a permanent operation. The C++ weak_ptr function offers numerous advantages for resource acquisition.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA