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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:15+00:00 2026-05-25T22:07:15+00:00

Possible Duplicate: In C++, why should new be used as little as possible? Is

  • 0

Possible Duplicate:
In C++, why should new be used as little as possible?

Is it really a bad idea to use ‘new’ in instantiating a class in C++? Found here.

I get that using raw pointers is ill-advised, but why have a ‘new’ keyword at all when it’s such bad practice? Or is it?

  • 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-25T22:07:16+00:00Added an answer on May 25, 2026 at 10:07 pm

    The point is that new, much like a pregnancy, creates a resource that is managed manually (i.e. by you), and as such it comes with responsibility.

    C++ is a language for library writing, and any time you see a responsibility, the “C++” approach is to write a library element that handles this, and only this, responsibility. For dynamic memory allocation, those library components already exist and are summarily referred to as “smart pointers”; you’ll want to look at std::unique_ptr and std::shared_ptr (or their TR1 or Boost equivalents).

    While writing those single-responsibility building blocks, you will indeed need to say new and delete. But you do that once, and you think about it carefully and make sure you provide the correct copy, assignment and destruction semantics. (From the point of exception safety, single responsibility is crucial, since dealing with more than one single resource at a time is horribly unscalable.)

    Once you have everything factored into suitable building blocks, you compose those blocks into bigger and bigger systems of code, but at that point you don’t need to exercise any manual responsibility any more, since the building blocks already do this for you.

    Since the standard library offers resource managing classes for the vast majority of use cases (dynamic arrays, smart pointers, file handles, strings), the point is that a well-factored and crafted C++ project should have very little need for any sort of manual resource management, which includes the use of new. All your handler objects are either automatic (scoped), or members of other classes whose instances are in turn scoped or managed by someone.

    With this in mind, the only time you should be saying new is when you create a new resource-managing object; although even then that’s not always necessary:

    std::unique_ptr<Foo> p1(new Foo(1, 'a', -2.5));   // unique pointer
    std::shared_ptr<Foo> p2(new Foo(1, 'a', -2.5));   // shared pointer
    auto p3 = std::make_shared<Foo>(1, 'a', -2.5);    // equivalent to p2, but better
    

    Update: I think I may have addressed only half the OP’s concerns. Many people coming from other languages seem to be under the impression that any object must be instantiated with a new-type expression. This is in itself a very unhelpful mindset when approaching C++:

    The crucial distinction in C++ is that of object lifetime, or “storage class”. This can be one of: automatic (scoped), static (permanent), or dynamic (manual). Global variables have static lifetime. The vast majority of all variables (which are declared as Foo x; inside a local scope) have automatic lifetime. It is only for dynamic storage that we use a new expression. The most important thing to realize when coming to C++ from another OO language is that most objects only ever need to have automatic lifetime, and thus there is never anything to worry about.

    So the first realization should be that “C++ rarely needs dynamic storage”. I feel that this may have been part of the OP’s question. The question may have been better phrased as “is it a really bad idea to allocate objects dynamically?”. It is only after you decide that you really need dynamic storage that we get to the discussion proper of whether you should be saying new and delete a lot, or if there are preferable alternatives, which is the point of my original answer.

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

Sidebar

Related Questions

Possible Duplicate: In C++, why should new be used as little as possible? Where
Possible Duplicate: PHPMailer AddAddress() Here is my code. require('class.phpmailer.php'); $mail = new PHPMailer(); $email
Possible Duplicate: When should I use the new keyword in C++? I don't know
Possible Duplicate: When should I use a javascript framework library? All, I'm fairly new
Possible Duplicate: When should I use the new keyword in C++? When should I
Possible Duplicate: What version of Python should I use if I’m a new to
Possible Duplicate: When should I create new Controller class in Asp.net MVC (Design Question)?
Possible Duplicate: Why use dynamic typing in c#? Should the new C# type 'dynamic'
Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: Why should the interface for a Java class be prefered? When should

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.