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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:57:09+00:00 2026-06-06T14:57:09+00:00

I think I might need to do something like the following pseudo-code in my

  • 0

I think I might need to do something like the following pseudo-code in my application:

boost::shared_ptr<T> p;
...
...
p = boost::shared_ptr<T>(new T);

I realize there are other options for using the assignment operator, but I noticed in the boost::shared_ptr documentation/example that they never discussed a case like this one involving the default constructor.

My question I suppose, is: is this valid use? In other words, is it legal and could it lead to potential memory leaks? Appreciate any ideas/corrections. Thanks much!

NOTE: I do recall reading, as also some comments below suggest, that they recommend to always use a named shared_ptr when invoking the constructor with new. Still, I find it hard to imagine this might cause any harm. Kindly post any counter-examples if you have them. Thanks!

  • 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-06T14:57:10+00:00Added an answer on June 6, 2026 at 2:57 pm

    It is perfectly legal and memory safe. The fact that you are invoking the default constructor on T is irrelevant: as long as you have a dynamically allocated object, you can wrap it in a shared_ptr<>

    If you refer to the default constructor of shared_ptr<> that’s fine too: the wrapped pointer is NULL, which will not be deleted when the other shared_ptr<> is assigned

    You will have to be careful, though to check whether the contents are NULL or not, otherwise you might be accessing an invalid pointer

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

Sidebar

Related Questions

For an application I'm working on I need something like a packing algorithm implemented
I think I might have done something right, headByRating and headByName both refer to
I think I might be having a stack overflow problem or something similar in
Today I think i might have learned something (not sure), i'm not really a
I am new to Unit Testing and think I might have dug myself into
I know Python (and a bunch of other languages) and I think it might
Long story short Say I have the following code: // a class like this
I would like to build a mobile application with the following requirements: The mobile
Consider following script (it's total nonsense in pseudo-language): if (Request.hostMatch(asfasfasf.com) && someString.existsIn(new String[] {brr,
You might think that this question is already answered. However, I couldn't find the

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.