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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:40+00:00 2026-05-23T19:31:40+00:00

In continuation to my previous question , I would like to ask the following

  • 0

In continuation to my previous question, I would like to ask the following :

Given a C++ function having a new statement in it but not returning anything explicitly (i.e. with a return statement), should it also always have a delete?

If no, could you please give me an example.

  • 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-23T19:31:41+00:00Added an answer on May 23, 2026 at 7:31 pm

    It doesn’t have to explicitly return the newly-created object, but it should pass it to something else in some way. Possible scenarios include:

    • In a member function, create an object and assign it to a field of the containing object.

    Example:

    class Foo {
        private:
            Baz* baz;
        public:
            Foo() : baz(0) { }
            void create_bar() { baz = new Baz(); }
            ~Foo() { delete baz; }
    };
    
    • Passing the new object to something else.

    Examples:

    void foo() {
        // assuming bar lives in the global scope
        bar.baz = new Baz();
    }
    
    void foo2(Bar& bar) {
        bar.baz = new Baz();
        // or, better:
        bar.setBaz(new Baz());
    }
    
    • Using some kind of garbage-collecting pointer type.

    Example:

    std::auto_ptr<Baz> foo() {
        return new Baz();
    }
    
    • Passing the pointer into another function that does something with it and then deletes it.

    Example:

    void foo(Bar* bar) {
        bar->dostuff();
        delete bar;
    }
    
    void baz() {
        Bar* bar = new Bar();
        foo(bar);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation of my previous question: Could not find an implementation of
This is a continuation from the previous stackoverflow jQuery question I asked, but I
This is sort of a continuation of my previous question , but I feel
This question is a continuation of my previous question here zend models architecture (big
This is a continuation question from a previous question I have asked I now
This question is a continuation of a previous thread to compare two lists with
This question is in continuation to my previous question, in which I asked about
This is a continuation from a previous stackoverflow question. I've renamed some variables so
This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't

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.