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

  • Home
  • SEARCH
  • 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 97655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:02:46+00:00 2026-05-11T00:02:46+00:00

Seem to be having an issue with std::auto_ptr and assignment, such that the object

  • 0

Seem to be having an issue with std::auto_ptr and assignment, such that the object referenced seems to get trashed for some reason.

std::auto_ptr<AClass> someVar = new AClass();  // should work, but mangles content std::auto_ptr<AClass> someVar( new AClass() ); // works fine. std::auto_ptr<AClass> someVar = std::auto_ptr<AClass>(new AClass()); // works fine.  std::auto_ptr<AClass> someVar; someVar.reset( new AClass() ); // works fine. 

I’ve traced it through, and it appears (via watching the values in the debugger) that the problem occurs in the transfer of the pointer from the temporary std::auto_ptr_byref() that is created to wrap the rhs pointer. That is the value contained in _Right on entering the auto_ptr(auto_ptr_ref<_Ty> _Right) function is correct, but the value in _Myptr on leaving is junk.

template<class _Ty>     struct auto_ptr_ref         {   // proxy reference for auto_ptr copying     auto_ptr_ref(void *_Right)         : _Ref(_Right)         {   // construct from generic pointer to auto_ptr ptr         }      void *_Ref; // generic pointer to auto_ptr ptr     };  template<class _Ty> class auto_ptr     {   // wrap an object pointer to ensure destruction public: typedef _Ty element_type;  explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()     : _Myptr(_Ptr)     {   // construct from object pointer     }  auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()     : _Myptr(_Right.release())     {   // construct by assuming pointer from _Right auto_ptr     }  auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()     {   // construct by assuming pointer from _Right auto_ptr_ref     _Ty **_Pptr = (_Ty **)_Right._Ref;     _Ty *_Ptr = *_Pptr;     *_Pptr = 0; // release old     _Myptr = _Ptr;  // reset this     } auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0() {   // assign compatible _Right._Ref (assume pointer) _Ty **_Pptr = (_Ty **)_Right._Ref; _Ty *_Ptr = *_Pptr; *_Pptr = 0; // release old reset(_Ptr);    // set new return (*this); } 

At first I thought it was messing up the inheritance and slicing off interfaces, however this happens even if the class only has one parent class.

We can avoid doing = new if we remember, either by using brackets or changing to have a explicit std::auto_ptr temp on the rhs, this is of course error prone.

Is it just this version of the library being broken, or some underlying thing I’m just not getting?

We also noticed a similar issue with assigning a std::auto_ptr to a boot::shared_ptr though we removed that entirely now and I don’t recall which syntax caused the issue.

  • 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. 2026-05-11T00:02:47+00:00Added an answer on May 11, 2026 at 12:02 am

    The first line:

    std::auto_ptr<AClass> someVar = new AClass();  // should work, but mangles content 

    should result in a compiler error. Because there is no implicit conversion from the raw AClass pointer to an auto_ptr (the constructor for an auto_ptr that takes a raw pointer is marked explicit), initialization using the ‘copy initializer’ form is not permitted.

    VC9 gives the following error:

    C:\temp\test.cpp(23) : error C2440: 'initializing' : cannot convert from 'AClass *' to 'std::auto_ptr<_Ty>' 

    Other compilers I’ve tried (GCC 3.4.5, Comeau C/C++ 4.3.10.1, Digital Mars) give a similar error.

    EDIT:

    It looks like this is in fact a bug in VS2005’s implementation of auto_ptr<> (not sure if it was introduced in SP1 or was in VS2005 from the start) that got fixed in VS2008. Here’s the MS Connect bug record for the problem:

    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842&wa=wsignin1.0

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

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably because your panel doesn't have a preferred size. When… May 12, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer You really don't need a regexp here. Here is a… May 12, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer The recommended way to do this is to just use… May 12, 2026 at 3:16 pm

Related Questions

I'm having an issue with expressions within reports. I'm coloring the background of a
I'm having an issue with django templates at the moment. I have 3 template
i'm having an issue with creating a query in oracle which doesnt seem to
I am having an issue that I can't seem to figure out. Hopefully somebody

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.