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

I've been experimenting with tracking area, and having some problems, so I created this
I created a web application that works on FLV files. This application uses a
This is more of a conceptual question and not about the underlying programming technique.
I started experimenting with C# and HTML5 today. I thought this question would be
I am experimenting with this code: foreach (var r in _vm.Rules.Take(20)) { Task.Factory.StartNew(() =>
I'm doing some experimenting with this malicious JavaScript line: var undefined = true; Every
I'm experimenting with knockout and the mapping plugin and wondering why this doesn't work.
I was experimenting a little with the C++11 standard and came up with this
I was experimenting with basic VB.Net File IO and String splitting. I encountered this
I'm experimenting playing video in a UIWebView. If I have some html like this:

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.