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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:41:29+00:00 2026-06-07T11:41:29+00:00

I am trying to use the auto_ptr in my code, but apparently something goes

  • 0

I am trying to use the auto_ptr in my code, but apparently something goes wrong.

auto_ptr<ClassType> Class(s.Build(aFilename)); //Instantiation of the Class object
int vM = s.GetM(Class);
int vS = s.Draw(Class);

The strange thing is that after the instantiation of Class, the Class object exist and so
by calling s.GetModelMean(Class), Class is not empty. But after exiting the function GetM,
Class is empty and so not usable anymore. A crash occurs when calling the function Draw.

I declared the functions the following way:

int GetM(auto_ptr<ClassType> aM); 

It seems that the class is destroyed, but I do not understand why…

  • 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-06-07T11:41:30+00:00Added an answer on June 7, 2026 at 11:41 am

    You shout not give an auto_ptr as an argument to a function. Either do:

    int GetM(const auto_ptr<ClassType>&)
    

    or

    int GetM(ClassType&)
    

    or

    int GetM(ClassType*)
    

    (may be also with const – depends what you are doing with it). The first you would call the same way, the second function you would call like this:

    int vM = s.GetM(*Class.get())
    

    the last without the star.

    The reason is: GetM will copy the auto_ptr (not the Class object) and destroy the auto_ptr when it returns. The auto_ptr (which is a scope pointer or unique pointer – not a reference counter!) will than destroy Class.

    Anyway: auto_ptr is very broken. Whenever possible (your compiler already supports some small parts of C++11), use std::unique_ptr and std::shared_ptr (last one does reference counting). unique_ptr will not allow you to mess around with it like that (since copying it is not allowed – which imho makes much more sense).

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

Sidebar

Related Questions

I'm trying to use smart pointers such as auto_ptr, shared_ptr. However, I don't know
At the following code I am trying to use Boost Spirit Auto Parser for
Trying to use autdoc extension of sphinx but its not working. i kept docs
Trying to use code from here Im trying to incorporate both classes so that
trying to use blueprint css framework, but can't understand why this is happening. I
I'm trying to use Qt Creator, but it is not working. My hello world
I currently trying to use an Object Relational Mapper for CodeIgniter and I'm experiencing
When trying to use an auto_ptr with a type that was declared with forward-declaration,
I'm trying to use for as an object property name. It seems to work
I'm trying to use openFileOutput from a class which is not Activity class. When

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.