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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:00:40+00:00 2026-05-29T08:00:40+00:00

In the following example, when I pass p to a function, It gets destroyed

  • 0

In the following example, when I pass p to a function, It gets destroyed as soon as the function func exits

void func(std::auto_ptr<int> p)
{
   // deletes p
}

int main()
{
    std::auto_ptr<int> p(new int);
    func(p);
    *p = 1; // run-time error
}

I’m also told that passing smart pointers by reference is very bad design from the book "The C++ Standard Library – Reference by Nicolai M. Josuttis".

Quote:

Allowing an auto_ptr to pass by reference is very bad design and you
should always avoid it …..

….. According to the concept of auto_ptrs, it is possible to transfer ownership into a function by using a constant reference.

Is it not possible to pass smart pointers or have I got the wrong idea?

  • 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-29T08:00:41+00:00Added an answer on May 29, 2026 at 8:00 am

    Is it not possible to pass smart pointers or have I got the wrong idea?
    It only applies to auto_ptr. Also, as per the new C++11 standard auto_ptr is deprecated and unique_ptr is the superior alternative if you are using c++11.

    The auto_ptr template class ensures that the object to which it points gets destroyed automatically when control leaves a scope, If you pass auto_ptr by value in a function, the object is deleted once the scope of the function ends. So essentially you transfer the ownership of the pointer to the function and you don’t own the pointer beyond the function call.

    Passing an auto_ptr by reference is considered a bad design because auto_ptr was specifically designed for transfer of ownership and passing it by reference means that the function may or may not take over the ownership of the passed pointer.


    In case of unique_ptr, If you pass an unique_ptr to function by value then you are transferring the ownership of the unique_ptr to the function.

    In case you are passing a reference of unique_ptr to the function if, You just want the function to use the pointer but you do not want to pass it’s ownership to the function.


    shared_ptr operates on a reference counting mechanism, so the count is always incremented when copying functions are called and decremented when a call is made to destructor.

    Passing a shared_ptr by reference avoids calls to either and hence it can be passed by reference. While passing it by value appropriately increments and decrements the count, the copy constructor for shared_ptr is not very expensive for most cases but it might matter in some scenarios, So using either of the two depends on the situation.

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

Sidebar

Related Questions

i run the following example from book c++ concurrency in action, and pass function
I have the following native function interface in C++: int func1(void* param, int sizeOfParam).
Can you pass by reference with R ? for example, in the following code:
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
How can I pass an argument in a function to create ID? For example
The main problem of this question is when we pass T into some function
The Phobos documentation shows the following example of ranges passed to a variadic function
I typed the following example: #include <iostream> double f(double* x, double* y) { std::cout
I need to pass a function as a parameter in Bash. For example, the
I want to pass a va_list through to another function. Here is an example

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.