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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:37:23+00:00 2026-06-16T14:37:23+00:00

This question may sound too simple to some guys, but I am trying to

  • 0

This question may sound too simple to some guys, but I am trying to understand what happens if I delete an object having a dynamically allocated block using delete keyword

Calling a delete will do 2 things, first call the destructor and then release the memory.
If delete is releasing the object memory then will it also release the dynamic allocated memory or would I have to write a block of code in destructor to actually release the memory block safely.

Also if objects are allocated memory in heap then what other things other than member variables will take up the memory allocated to the object.

Thanks in advance

  • 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-06-16T14:37:24+00:00Added an answer on June 16, 2026 at 2:37 pm

    If you have something like this:

    class Foo
    {
    public:
       int* block;
    
       Foo()
       {
           block = new int[10];
       }
    
    private:
       Foo(const Foo&);
       Foo& operator =(const Foo&);
    };
    

    And subsequently do this:

    Foo* foo = new Foo;
    delete foo;
    

    Then yes, you’re leaking memory. The dynamic block in your Foo object is never released. You can address this with a destructor that releases it. (of source, you need to declare the destructor in the class declaration as well):

    Foo::~Foo()
    {
        delete [] block;
    }
    

    I would advise you to do two things

    1. Count the delete’s and the new’s. if they’re not the same, thats generally a problem.
    2. Read this informative document on dynamic pointer and memory usage with C++.

    Following #2, btw, might give you an object that looks similar to this:

    class Foo
    {
    public:
       std::array<int,10> block;
    
       Foo() // note: default-construction of `block`
       {
       }
    
       // note: default *destructor* will clean up member variables
       //  by firing their destructors for you. in this case the destructor
       //  for our 'block' member is a std::array that knows how to self-clean.
    
       // note: we no longer have to hide or implement copy construction and
       //  assignment operator functionality. The default implementation of 
       //  these properly member-copy and member-assign respectively.
    };
    

    And a usage (one of many possibilities) like this:

    std::unique_ptr<Foo> foo(new Foo);
    

    Please regard the notes in the source of the last example. There is a tremendous weight lifted off your memory-management shoulders by using classes that practice self-managed members. In lifting that weight, so too goes the per-chance likelihood of introducing bugs related to it, like memory leaks, shallow-copy perils, etc.

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

Sidebar

Related Questions

I understand this question may sound very basic but I have been trying to
This may sound like a stupid question, perhaps it is. But I'm only trying
This question may sound silly.. but my intention is to understand advantages of Java
I know this question may sound silly, but it happened. I put some statements
This may sound like a simple question, but I just cannot seem to find
i know this question may sound stupid to you guys, but i am a
This may not sound like a coherient question, but I need to find some
I know this question may sound silly, but I am learning (at least trying)
This may sound like a newbie question, but I really need some help with
This question may sound too silly, however , I don't find concrete answer any

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.