Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4274226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:49:07+00:00 2026-05-21T07:49:07+00:00

I understand how to use weak_ptr and shared_ptr . I understand how shared_ptr works,

  • 0

I understand how to use weak_ptr and shared_ptr. I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I’m not familiar enough with boost to understand all the things it uses.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:49:07+00:00Added an answer on May 21, 2026 at 7:49 am

    shared_ptr uses an extra “counter” object (aka. “shared count” or “control block”) to store the reference count.
    (BTW: that “counter” object also stores the deleter.)

    Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the “counter” object.

    To implement weak_ptr, the “counter” object stores two different counters:

    • The “use count” is the number of shared_ptr instances pointing to the object.
    • The “weak count” is the number of weak_ptr instances pointing to the object, plus one if the “use count” is still > 0.

    The pointee is deleted when the “use count” reaches zero.

    The “counter” helper object is deleted when the “weak count” reaches zero (which means the “use count” must also be zero, see above).

    When you try to obtain a shared_ptr from a weak_ptr, the library atomically checks the “use count”, and if it’s > 0 increments it. If that succeeds you get your shared_ptr. If the “use count” was already zero you get an empty shared_ptr instance instead.


    EDIT: Now, why do they add one to the weak count instead of just releasing the “counter” object when both counts drop to zero? Good question.

    The alternative would be to delete the “counter” object when both the “use count” and the “weak count” drop to zero. Here’s the first reason: Checking two (pointer sized) counters atomically is not possible on every platform, and even where it is, it’s more complicated than checking just one counter.

    Another reason is that the deleter must stay valid until it has finished executing. Since the deleter is stored in the “counter” object, that means the “counter” object must stay valid. Consider what could happen if there is one shared_ptr and one weak_ptr to some object, and they are reset at the same time in concurrent threads. Let’s say the shared_ptr comes first. It decreases the “use count” to zero, and begins executing the deleter. Now the weak_ptr decreases the “weak count” to zero, and finds the “use count” is zero as well. So it deletes the “counter” object, and with it the deleter. While the deleter is still running.

    Of course there would be different ways to assure that the “counter” object stays alive, but I think increasing the “weak count” by one is a very elegant and intuitive solution. The “weak count” becomes the reference count for the “counter” object. And since shared_ptrs reference the counter object too, they too have to increment the “weak count”.

    A probably even more intuitive solution would be to increment the “weak count” for every single shared_ptr, since every single shared_ptr hold’s a reference to the “counter” object.

    Adding one for all shared_ptr instances is just an optimization (saves one atomic increment/decrement when copying/assigning shared_ptr instances).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how the isgraph() function works in C? I understand its use
I keep coming across the use of this word and I never understand its
I understand the use for explicitly typed self-references : trait T { self :
I am reading about MessageContract & MessageHeader and trying to understand the use case
I am trying to understand the use of export command. I tried using man
i am trying to understand the use of exceptions in PHP.. How they work
I can understand the use for one level of namespaces. But 3 levels of
I want to understand the use of xargs man in Rampion's code : screen
I don't really understand the use and concept of an Intent. I DO understand
I am learning Django and I am trying to understand the use of models.py

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.