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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:29:44+00:00 2026-05-24T18:29:44+00:00

For example: boost::shared_ptr<int> test() { boost::shared_ptr<int> x(new int(3)); return x; } void function() {

  • 0

For example:

boost::shared_ptr<int> test() {
    boost::shared_ptr<int> x(new int(3));
    return x;
}

void function() {
    int y = *test();
    ...
}

Is it also a bad idea to use shared_ptr to avoid copying the whole object? Like a vector of matrices/images for example.

  • 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-24T18:29:46+00:00Added an answer on May 24, 2026 at 6:29 pm

    In the general case, no. Your example copies the contents of the shared_ptr, and then the original value is deleted.

    Now, the bigger issue here is that it’s fantastically inefficient to do a dynamic memory allocation for an int, but I’m assuming you’re not doing that in real code. 🙂

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

Sidebar

Related Questions

class Example { boost::shared_ptr<FilterProvider> filterProvider; public: void RegisterFilter(const boost::shared_ptr<FilterProvider>& toRegister) { filterProvider = toRegister;
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
I have this example code: #include template<class T> class Class { public: typedef boost::shared_ptr<Class<T>
This question also applies to boost::function and std::tr1::function . std::function is not equality comparable:
What is the best practice when returning a smart pointer, for example a boost::shared_ptr?
Please consider the following simplified example: #include boost/shared_ptr.hpp #include boost/smart_ptr/enable_shared_from_this.hpp using namespace boost; class
For example, class Segment { friend bool someFunc( P_Segment p ); }; typedef boost::shared_ptr<Segment>
When attempting to run the first example in the boost::mpi tutorial , I was
I am trying to run blocking_udp_echo_server.cpp from Boost asio example on MacOSX 10.5. But
I'am trying to understand the example from program_options of the boost library ( http://www.boost.org/doc/libs/1_38_0/doc/html/program_options/tutorial.html#id3761458

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.