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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:06:25+00:00 2026-05-27T03:06:25+00:00

I am not experienced in handling of the memory in a C++ program, so

  • 0

I am not experienced in handling of the memory in a C++ program, so I would like a piece of advice in that case:

I want to create a new Object in a function in a class which is essential till the end of the program. As far as I am concerned, if I use the operator new, I should sometimes delete it. Taking into account that it must be initialized inside a class, when and how must I finally delete 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-27T03:06:25+00:00Added an answer on May 27, 2026 at 3:06 am

    Edit: Using some sort of smart pointer is often a good idea, but I believe it is still essential to have a solid understanding of manual memory management in C++.

    If you want an object in a class to persist until the end of the program, you can simply make it a member variable. From what you’ve said, there’s nothing to suggest you need to use new or delete here, just make it an automatic variable. If you did want to use new and delete for practice, you should read up on constructors and destructors for a class (you can and will use new and delete outside of classes, but I’m trying to keep this relevant to your question). Here’s one I prepared earlier:

    class Foo
    {
        public:
            Foo();  // Default constructor.
            ~Foo(); // Destructor.
    
        private:
            int *member;
    }
    
    Foo::Foo() // Default constructor definition.
    {
        member = new int; // Creating a new int on the heap.
    }
    
    Foo::~Foo() // Destructor.
    {
        delete member; // Free up the memory that was allocated in the constructor.
    }
    

    This is a simple example, but it will hopefully help you out. Note that the variable will only persist as long as the object is alive. If the object is destroyed or goes out of scope, the destructor will be called and the memory will be freed.

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

Sidebar

Related Questions

Question related to PHP memory-handling from someone not yet very experienced in PHP: If
OK, I am not that experienced with SQL 2005 error handling and am learning
Sorry, I'm not terribly experienced with Ant. I like the eclipse Export ant buildfile
I need help on regex or preg_match because I am not that experienced yet
I'm running my C++ program in gdb. I'm not real experienced with gdb, but
I would like to get some advice on structuring my javascript code and jquery
I have an application that's having some trouble handling multi-processor systems. It's not an
Background I'm not new to programming, however I am when it comes to handling
I'm not an experienced C++ programmer and I'm having problems compiling. I've got a
( Updated a little ) I'm not very experienced with internationalization using PHP, it

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.