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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:53:50+00:00 2026-06-11T10:53:50+00:00

The more I read about RAII, I understand that using the stack is the

  • 0

The more I read about RAII, I understand that using the stack is the way to make sure that the code is exception safe.

Does that mean every time I am doing a new() in my code, I am doing something wrong in the sense there is a better way to do it using the RAII principle?

  • 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-11T10:53:51+00:00Added an answer on June 11, 2026 at 10:53 am

    You’re not necessarily doing something wrong if you use new, but it’s worth checking that you’re doing it right.

    • The result of the new expression should be immediately placed under the control of a smart pointer, usually by passing it straight into the constructor.
    • If that smart pointer is a shared_ptr, then you are probably doing it wrong. You should probably use make_shared instead. There are some situations where you shouldn’t (use of weak_ptr to large objects), and some where you can’t (C++03 without Boost).
    • If you use delete then you pretty much are doing it wrong, unless you are writing your own smart pointer class. And even then, your smart pointer might be able to use another smart pointer to save work.
    • This is not essential, but if you use new solely because the object is “too big for the stack”, consider writing a class that acts as a handle to the object, using unique_ptr or scoped_ptr to manage it, so that from the user’s point of view the objects they deal with are automatic variables. If you feel like it, you can extend this to the full PImpl idiom. Even if you don’t want another class, consider a function that creates the object and returns a unique_ptr to it, which you can then call like auto foohandle = give_me_a_foo();. Then give_me_a_foo contains new, but other user code doesn’t, and you encourage the practice of automatically stuffing things into RAII objects.

    There are alternative resource-management strategies to RAII in C++, but you’d know about it if you were using them, and that would affect what counts as “wrong”.

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

Sidebar

Related Questions

I am learning JPA. I read about persistence.xml file. It can contain more that
I would like to read a little more about the + that is usually
The more I read about frameworks, the more people seem to complain that they're
Where can I read more about creating linked slides like at the top of
Possible Duplicate: What is a ‘Closure’? I read more and more about closures but
I read in more than one website about this method of protecting forms: I
I'm trying to learn more about WPF and I've read a bit about Model-View-ViewModel
I need more information about the Jruby on rails framework. I have read a
So I'm trying to learn more about lambda expressions. I read this question on
where can i read more about cakephp 2.0 features? cause i could find symfony

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.