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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:59:12+00:00 2026-05-30T14:59:12+00:00

I was reading about smart pointers in C++ and saw this example given as

  • 0

I was reading about smart pointers in C++ and saw this example given as how smart pointers handle “dangling pointer” problem if normal pointers are used. P.S. I know auto_ptr is deprecated by C++ 2011 but it still has unique_ptr or some such equivalent of it.

Dangling pointers. A common pitfall of regular pointers is the dangling pointer: a pointer that points to an object that is already deleted. The following code(jsut for example purpose) illustrates this situation:

    MyClass* p(new MyClass);//Assume we have a MyClass definition available
    MyClass* q = p;
    delete p;
    p->DoSomething();   
    p = NULL;           // p is not dangling anymore
    q->DoSomething();   // Don't.. q is still dangling!

Using auto_ptr, this is solved by setting its pointer to NULL when it is copied (Copy constructor implemented as below):

template <class T>
auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<T>& rhs)
{
    if (this != &rhs) {
        delete ptr;
        ptr = rhs.ptr;
        rhs.ptr = NULL;
    }
    return *this;
}

And now if we have the same code using auto_ptr as below:

auto_ptr<MyClass> p(new MyClass);
auto_ptr<MyClass> q = p;
delete p;
p->DoSomething();   
p = NULL;           
q->DoSomething();   

I get the logic in the aqbove copy constructor but how does making the rhs.ptr = NULL help. I mean if one dereferences this pointer later (q in above code example), it would crash as it is made NULL by the smart pointer copy constructor.

Where as what would have happened if it was a normal pointer and we would have dereferenced a dangling pointer? Some undefined behavior maybe. But how does using auto_ptr help

  • 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-30T14:59:13+00:00Added an answer on May 30, 2026 at 2:59 pm

    Smart pointers won’t necessarily protect you if you dereference them without checking them first; what they give you is a guarantee that you can check whether they are valid before dereferencing.

    The problem with a dangling pointer is that it doesn’t point to anything valid, but there’s no way to tell:

    Thing * p = new Thing;
    Thing * q = p;
    
    if (p) p->DoSomething(); // OK: checks p, calls function
    if (q) q->DoSomething(); // OK: checks q, calls function
    
    delete p;
    p = nullptr;
    
    if (p) p->DoSomething(); // OK: checks p, does nothing
    if (q) q->DoSomething(); // BOOM! Undefined behaviour
    

    With a smart pointer, you’ll never have that situation; either the pointer is valid, or you can tell that it’s invalid before dereferencing:

    auto_ptr<Thing> p(new Thing);
    auto_ptr<Thing> q = p;
    
    if (p) p->DoSomething(); // OK: checks p, does nothing
    if (q) q->DoSomething(); // OK: checks q, calls function
    
    p.reset(); // does nothing
    
    if (p) p->DoSomething(); // OK: checks p, does nothing
    if (q) q->DoSomething(); // OK: checks q, calls function
    
    q.reset(); // deletes the object
    
    if (p) p->DoSomething(); // OK: checks p, does nothing
    if (q) q->DoSomething(); // OK: checks q, does nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
general questions Now I've been reading quite a bit about smart pointers, and shared
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
Reading about the G.729 codec , I found this interesting tidbit about Comfort Noise
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
I was reading about how to implement a DPDA and found this code in
I'm about to start learning Ruby and would like a bit of reading material
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
When reading about SQL Injection and XSS i was wondering if you guys have

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.