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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:27:29+00:00 2026-05-26T11:27:29+00:00

This is just an academic question (I would never do this in real code):

  • 0

This is just an academic question (I would never do this in real code):

If I were to use shared_ptr<> universally in my code, would the behavior be equivalent to a gc-collected language like Java?

If not, how would the behavior be different from a gc-embedded language? Which C++ construct would yield equivalent behavior compared to a gc-embedded language?

Note: In real coding, I strongly prefer the use of RAII and strict ownership over the use of any smart pointers. I also know that other less-generic pointers, unique_ptr<> would be more efficient. This question is just a query into smart-pointer equivalence.

  • 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-26T11:27:30+00:00Added an answer on May 26, 2026 at 11:27 am

    No, there’d be a couple of important differences:

    • You would get a memory leak any time you have a cyclic reference. A garbage collector can handle cycles, ref-counting can’t.
    • You would avoid any stalls or pauses because no garbage collection ever occurs. On the other hand, you’d likely spend more total CPU time cleaning up resources, because the amortized cost of an occasional garbage collection is pretty low, and ref-counting can be relatively expensive if you do it on everything.

    Obviously the first point is the killer. If you did this, many of your resources wouldn’t get freed, and you’d leak memory and your app just wouldn’t behave very well.

    Which C++ construct would yield equivalent behavior compared to a gc-embedded language?

    None. C++ doesn’t have a garbage collector because there’s no way to implement a correct, reliable one. (Yes, I’m aware of Boehm’s GC, and it’s a good approximation, but it’s conservative, and doesn’t detect all references, only the ones it can be 100% sure of. There is no way, in a general C++ program, to implement a garbage collector that Just Works(tm))

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

Sidebar

Related Questions

Just an academic question. I'm curious what version of this code is better (faster)
This question is purely academic, because I'd never dream of doing this in real
Is this just impossible? I thought I would clean up some of my code
Just so it's known, this question is mostly academic, even though I tried to
This is more of an academic question about performance than a realistic 'what should
This just seems absurd to me. Should I use array instead or is there
This is a bit more of an academic question. Indeed I am preparing for
I want to implement another debugger (language) for .NET (it's just for academic reason
Somewhat of an academic question, but I ran into this while writing some unit
This is a rather academic question, I realise it matters little regarding optimization, but

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.