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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:07:54+00:00 2026-05-18T06:07:54+00:00

This is merely for curiosity sake because I have not used new and delete

  • 0

This is merely for curiosity sake because I have not used new and delete in c++ except for the most basic uses.

I know that delete releases memory. The thing I’m wondering is how does it handle more complex cases?

For instance, if I have a user-defined class like this:

class MyClass
{
public:
    MyClass();
    ~MyClass()
    {
        delete [] intArray;
    }
    //public members here
private:
    int* intArray;
};

Assuming the class allocates memory somehow for intArray, and then release it in the destructor, What if I used the class like this: MyClass* myClass = new MyClass(); and released it later with delete myclass;

How does delete handle the releasing of all the memory? Does the class destructor get called first to release all of the memory allocated by the class (ie int* intArray) and then release the memory allocated to hold the class?

What if I had a class like this:

class MyClass
{
public:
    MyClass();
    ~MyClass()
    {
        delete anotherMyClass;
    }
    //public members here
private:
    MyClass* anotherMyClass;
};

Assuming anotherMyClass is not allocated with the constructor, which would use up memory very quickly, what if there was a chain of MyClasses attached to each other like a linked-list? Would the delete statement in the destructor work in this case? Would each anotherMyClass be recursively released when the destructor gets called?

Are there any specific weird tricks or caveats with the new and delete statements that you know about?

  • 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-18T06:07:55+00:00Added an answer on May 18, 2026 at 6:07 am

    Given a pointer (p) to a dynamically allocated object, delete does two things:

    1. It calls the destructor of the dynamically allocated object. Note that when ~MyClass() completes, the destructors for any member variables of class type are called.
    2. It frees the memory occupied by the dynamically allocated object.

    It doesn’t search the member variables of the object for other pointers to free; it doesn’t free any other memory and doesn’t do anything else.

    If you need to free the memory pointed to by intArray, you need to delete it in the destructor of MyClass.

    However, in almost all C++ code, you don’t need to worry about this. You should be using smart pointers like shared_ptr, unique_ptr, auto_ptr, and scoped_ptr to automatically manage dynamically allocated objects. Manual resource management is difficult at best and should be avoided wherever possible.

    This is part of a broader idiom, Scope-Bound Resource Management (SBRM, also called Resource Acquisition is Initialization, or RAII). This is by far the most important design pattern to understand and to use everywhere in your C++ code.

    If in your class you had declared this instead:

    boost::scoped_ptr<int> intArray;
    

    then when the scoped_ptr<int> object is destroyed, it will free the pointer that it holds. You then do not have to do any work to manually destroy the object.

    In well-written, modern C++ code, you should rarely need to manually use delete. Smart pointers and other SBRM containers should be used to manage any type of resource that needs cleanup, including dynamically allocated objects.


    In your second example, given a linked list that looks like:

    x -> y -> z -> 0
    

    you would have an order of operations that looks like this:

    delete x;
      x.~MyClass();
        delete y;
          y.~MyClass();
            delete z;
              z.~MyClass();
                delete 0;
              [free memory occupied by z]
          [free memory occupied by y]
      [free memory occupied by x]
    

    The objects in the linked list would be destroyed in reverse order.

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

Sidebar

Related Questions

this is not a primary key, but merely used as a marker in existing
This is not essential for my programs, but merely out of curiosity. Is it
My knowledge of matlab is merely on a need to know basis, so this
I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse
This problem has been bugging me since forever. I have an array and in
This is a weired problem, I have implemented simple quick sort as follows.. static
This is my bitmap object Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed); BitmapData bmd
This should be exceptionally simple. I have a zoo object with 500 times series
This question is merely about algorithm. In pseudo code is like this: A =
MSDN states of the property TypeId that: As implemented, this identifier is merely the

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.