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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:12:32+00:00 2026-05-18T11:12:32+00:00

In experimenting with this question I created an example that I utterly do not

  • 0

In experimenting with this question I created an example that I utterly do not understand. In particular, it highlights my misunderstanding of pointers, references, and the boost::shared_ptr.

int& r = *(new int(0));//gratuitous pointer leak, got to initialize it to something
{
    boost::shared_ptr<int> sp(new int(100));
    r = *sp;
    cout << "r=" << r << endl;
}
cout << "r=" << r << endl << endl;

int* p;
{
    boost::shared_ptr<int> sp(new int(100));
    p = &*sp;
    cout << "*p=" << *p << endl;
}
cout << "*p=" << *p << endl;

Running this code gives an output something like this:

r=100
r=100

*p=100
*p=13

Why does the reference survive the death of the shared_ptr but the pointer does not?


There’s a problem in the answers here in that there seem to be two diametrically opposed and contradictory solutions and no consensus upon which is the truth. I would like the ability to use a reference after a shared_ptr is deleted, but if it’s invalid I really need to understand this.

Perhaps someone can post a simple example that demonstrates the undefined behavior in the reference.

  • 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-18T11:12:32+00:00Added an answer on May 18, 2026 at 11:12 am

    Because r = *sp; does not do what you think it does. It assigns to the referent, that is, to the anonymous int object you created on the heap in line 1. You cannot reseat references in C++.

    Here is what the standard says about evaluating reference expressions:

    If an expression initially has the type “reference to T“,
    the type is adjusted to T prior to any further analysis.
    The expression designates the object or function denoted by the reference,
    and the expression is an lvalue or an xvalue, depending on the expression.

    So you see, there is no way to get to “the reference itself”. It simply does not exist in C++.

    Maybe this code will make it clearer:

    int a = 42;
    int b = 97;
    
    int&r = a;   // r is just an alias (another name) for a
        r = b;   // assigns b to a (does NOT bind r to b, that's impossible in C++!)
    

    After executing the last line, both a and b contain 97, because r = b really means a = b.

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

Sidebar

Related Questions

While experimenting with this question on collections in Spring.NET , I discovered that Spring
This is probably a silly question. I am experimenting with python doctest, and I
As in this question , I'm experimenting to stream via a class using SBRM/RAII,
I've been experimenting with ASP.NET MVC and following this tutorial to create the basic
I can't seem to figure this out. I'm experimenting with MVC Beta and am
I'm experiencing this weird problem which my scrollbar jumps by itself to somewhere that
I've been pondering this question for a while: Can you build a faster fundamental
I am experiencing a strange behavior with very basic web service development. This question
Recently, I've started experimenting with Mercurial, due to the fact that it always attracted
This is quick confirmation question: In order to make partial classes work, I initially

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.