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 6852245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:20:52+00:00 2026-05-27T01:20:52+00:00

general questions Now I’ve been reading quite a bit about smart pointers, and shared

  • 0

general questions
Now I’ve been reading quite a bit about smart pointers, and shared pointers seem like “perfect” in many cases. However I also read about cyclical reference or something like that? Where shared_ptr can’t be used? I’m having a difficult time undestanding this, can someone give a trivial example showing this?

Also I’m really wondering, what do weak_ptr’s provide that normal pointers don’t? – As they don’t increase the reference count they give no guarantee that the memory they point at is still valid?

my personal project:
In a project I’ve 2 “global” containers (both containers are soon to be moved inside a class), both are filled with “objects”. However both should “point” to the same object. An object can not exist outside those containers, and it should not be possible that one container does contain it, while the other doesn’t.

Currently I simply use normal pointers for this, and have a createObject& destroyObject method to manage the memory.

Is this good design? Should I use smart pointers?

  • 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-27T01:20:53+00:00Added an answer on May 27, 2026 at 1:20 am

    To answer your various questions:

    Cyclical references are when 2 different objects each have a shared_ptr to the other object.

    For example:

    struct Foo {
         shared_ptr< Bar > m_bar;
    };
    
    struct Bar {
         shared_ptr< Foo > m_foo;
    };
    
    
    void createObject()
    {
        shared_ptr< Foo > foo( new Foo );
        shared_ptr< Bar > bar( new Bar );
        foo->m_bar = bar;
        bar->m_foo = foo;
        //Neither of these objects will be released here
    }
    

    This can result in neither of the objects being free’d, as Foo will always keep the reference count to bar above 1, and foo won’t be free’d because bar will always keep it’s reference count above 1.

    This is a situation that can be overcome with weak_ptr’s as they do not increment the reference count. As you point out, this will not stop the ptr from being free’d, but does allow you to check that the object still exists before using it, which you could not do with a standard pointer.

    As for the example you provided, you should almost always use smart pointers rather than raw pointers, as they allow objects to be free’d automatically when they go out of scope, rather than you having to ensure it is done yourself, which can be error prone. This is especially true in the case where you have exceptions, which could easily skip over any releasing code that you’ve written.

    For example, this code could cause problems:

    Foo* foo = createObject();
    foo.doSomething();
    deleteObject( foo );
    

    If foo.doSomething was to except, then deleteObject would never be called and foo would not be free’d.

    However, this would be safe:

    shared_ptr< Foo > foo = createObject();
    foo.doSomething();
    

    The shared_ptr will automatically be released at the end of the code block, regardless of whether an exception has occurred.

    There’s a fairly good discussion of pointers and smart pointers here: Pointers, smart pointers or shared pointers?

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

Sidebar

Related Questions

Now I know there are quite a few questions regarding this but in general
Reading through some of the questions here, the general concensus seems to be that
I've been working on this for a while now and I can't seem to
I created a new email box for general support questions. When I try to
I have found several other questions here on S.O. (and the web in general)
general question is i like to build logger class that writes to single log
Just a general question about what the best practice is: public void Foo() {
Ok this is a general question about how to solve this issue, not to
This is a general question about MVC as a pattern, but in this case
Yesterday I asked this general question about decimals and their internal precisions. Here is

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.