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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:35:48+00:00 2026-05-13T10:35:48+00:00

Reading through an old C++ Journal I had, I noticed something. One of the

  • 0

Reading through an old C++ Journal I had, I noticed something.

One of the articles asserted that

Foo *f = new Foo();

was nearly unacceptable professional C++ code by and large, and an automatic memory management solution was appropriate.

Is this so?

edit: rephrased: is direct memory management unacceptable for new C++ code, in general? Should auto_ptr(or the other management wrappers) be used for most new code?

  • 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-13T10:35:48+00:00Added an answer on May 13, 2026 at 10:35 am

    This example is very Java like.
    In C++ we only use dynamic memory management if it is required.
    A better alternative is just to declare a local variable.

    {
        Foo    f;
    
        // use f
    
    } // f goes out of scope and is immediately destroyed here.
    

    If you must use dynamic memory then use a smart pointer.

    // In C++14
    {
        std::unique_ptr<Foo>  f = std::make_unique<Foo>(); // no need for new anymore
    }
    
    // In C++11
    {
        std::unique_ptr<Foo>  f(new Foo);  // See Description below.
    }
    
    // In C++03
    {
        std::auto_ptr<Foo>    f(new Foo);  // the smart pointer f owns the pointer.
                                           // At some point f may give up ownership to another
                                           // object. If not then f will automatically delete
                                           // the pointer when it goes out of scope..
    
    }
    

    There are a whole bunch os smart pointers provided int std:: and boost:: (now some are in std::tr1) pick the appropriate one and use it to manage the lifespan of your object.

    See Smart Pointers: Or who owns you baby?

    Technically you can use new/delete to do memory management.
    But in real C++ code it is almost never done. There is nearly always a better alternative to doing memory management by hand.

    A simple example is the std::vector. Under the covers it uses new and delete. But you would never be able to tell from the outside. This is completely transparent to the user of the class. All that the user knows is that the vector will take ownership of the object and it will be destroyed when the vector is destroyed.

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

Sidebar

Related Questions

I'm reading through some old code at work, and have noticed that there are
Reading through some of the questions here, the general concensus seems to be that
Reading through the SendAsync , BeginAsync method illustrations of Socket s, I realized that
It's clear from reading through threads that I can call a PHP function using
Reading through an old blog post on SAP's community network, I found some instructions
I am assigned some old code and when I was reading through it, I
I'm reading through some MSDN example code on Composite controls and finding that there
I am still very new to Ruby (reading through the Pickaxe and spending most
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select

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.