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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:53:10+00:00 2026-05-13T06:53:10+00:00

Background : When reading Dr. Stroustrup’s papers and FAQs, I notice some strong opinions

  • 0

Background: When reading Dr. Stroustrup’s papers and FAQs, I notice some strong “opinions” and great advices from legendary CS scientist and programmer. One of them is about shared_ptr in C++0x. He starts explaining about shared_ptr and how it represents shared ownership of the pointed object. At the last line, he says and I quote:

. A shared_ptr represents shared
ownership but shared ownership isn’t
my ideal: It is better if an object
has a definite owner and a definite,
predictable lifespan.

My Question: To what extent does RAII substitute other design patterns like Garbage Collection? I am assuming that manual memory management is not used to represent shared ownership in the system.

  • 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-13T06:53:11+00:00Added an answer on May 13, 2026 at 6:53 am

    To what extent does RAII substitute other design patterns like Garbage Collection? I am assuming that manual memory management is not used to represent shared ownership in the system

    Hmm, with GC, you don’t really have to think about ownership. The object stays around as long as anyone needs it. Shared ownership is the default and the only choice.

    And of course, everything can be done with shared ownership. But it sometimes leads to very clumsy code, because you can’t control or limit the lifetime of an object. You have to use C#’s using blocks, or try/finally with close/dispose calls in the finally clause to ensure that the object gets cleaned up when it goes out of scope.

    In those cases, RAII is a much better fit: When the object goes out of scope, all the cleanup should happen automatically.

    RAII replaces GC to a large extent. 99% of the time, shared ownership isn’t really what you want ideally. It is an acceptable compromise, in exchange for saving a lot of headaches by getting a garbage collector, but it doesn’t really match what you want. You want the resource to die at some point. Not before, and not after. When RAII is an option, it leads to more elegant, concise and robust code in those cases.

    RAII is not perfect though. Mainly because it doesn’t deal that well with the occasional case where you just don’t know the lifetime of an object. It has to stay around for a long while, as long as anyone uses it. But you don’t want to keep it around forever (or as long as the scope surrounding all the clients, which might just be the entirety of the main function).

    In those cases, C++ users have to “downgrade” to shared ownership semantics, usually implemented by reference-counting through shared_ptr. And in that case, a GC wins out. It can implement shared ownership much more robustly (able to handle cycles, for example), and more efficiently (the amortized cost of ref counting is huge compared to a decent GC)

    Ideally, I’d like to see both in a language. Most of the time, I want RAII, but occasionally, I have a resource I’d just like to throw into the air and not worry about when or where it’s going to land, and just trust that it’ll get cleaned up when it’s safe to do so.

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

Sidebar

Related Questions

Thanks for reading! Some background: I am building a Gallery app from the tutorial
I have an application which does some background task (network listening & reading) in
I am new to C++ from a C#.NET and Visual Studio background (and some
I'm reading some GPS data (longitude,latitude) from a ServerManager class that stores it. The
I'm using a background thread to read from a Sqlite database some GPS data...and
Some background: For a project I'm working on (building an XML DOM from a
This maybe a stupid question but i've come from an asp.net background so reading
Some background for people reading this in the future (in case it's not locked).
Background (question further down) I've been Googling this back and forth reading RFCs and
For a bit of background, I'm writing a meter reading application in C for

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.