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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:33:01+00:00 2026-05-29T06:33:01+00:00

From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9 Remember: delete p does two things: it calls the destructor

  • 0

From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9

Remember: delete p does two things: it calls the destructor and it deallocates the memory.

If delete deallocates the memory, then what’s the need of the destructor here?

  • 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-29T06:33:02+00:00Added an answer on May 29, 2026 at 6:33 am

    You need to call the destructor in case there are other things that need to be done other than just de-allocating memory.

    Other than very simple classes, there usually are.

    Things like closing file handles or shutting down database connections, deleting other objects that are pointed to by members data within your object, and so forth.

    A classic example is the implementation of a stack:

    class myStack {
        private:
            int *stackData;
            int topOfStack;
    
        public:
            void myStack () {
                topOfStack = 0;
                stackData = new int[100];
            }
    
            void ~myStack () {
                delete [] stackData;
            }
    
            // Other stuff here like pop(), push() and so on.
    }
    

    Now think of what would happen if the destructor was not called every time one of your stacks got deleted. There is no automatic garbage collection in C++ in this case so the stackData memory would leak and you’d eventually run out.


    This requiring of a destructor to delete all its resources moves down the tree towards the basic types. For example, you may have a database connection pool with an array of database connections. The destructor for that would delete each individual database connection.

    A single database connection may allocate a lot of stuff, such as data buffers, caches, compiled SQL queries and so on. So a destructor for the database connection would also have to delete all those things.

    In other words, you have something like:

    +-------------------------------------+
    | DB connection pool                  |
    |                                     |
    | +-------------------------+---+---+ |
    | | Array of DB connections |   |   | |
    | +-------------------------+---+---+ |
    |                             |   |   |
    +-----------------------------|---|---+
                                  |   |   +---------+
                                  |   +-> | DB Conn |
                 +---------+      |       +---------+
                 | DB Conn | <----+         /  |  \
                 +---------+         buffers   |   queries
                   /  |  \                  caches
            buffers   |   queries
                   caches
    

    Freeing the memory for the DB connection pool would not affect the existence of the individual DB connection or the other objects pointed to by them.

    That’s why I mentioned that only simple classes can get away without a destructor, and those are the classes that tend to show up at the bottom of that tree above.

    A class like:

    class intWrapper {
        private:
            int value;
        public:
            intWrapper () { value = 0; }
            ~intWrapper() {}
            void setValue (int newval) { value = newval; }
            int getValue (void) { return value; }
    }
    

    has no real need for a destructor since the memory deallocation is all you need to do.


    The bottom line is that new and delete are opposite ends of the same pole. Calling new first allocates the memory then calls the relevant constructor code to get your object in a workable state.

    Then, when you’re done, delete calls the destructor to “tear down” your object the reclaims the memory allocated for that object.

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

Sidebar

Related Questions

From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5 A member (either data member or member function) declared in a protected
The question is from C++ faq. http://www.parashift.com/c++-faq-lite/protected-virtuals.html Code using public overloaded virtuals: class Base
From http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.14 : Even if the language outlawed const_cast , the only way to
From: http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.9 Three keys: ROI, ROI and ROI. Every interface you build has a
I have a question based on this question In the section http://www.parashift.com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3 the following
http://www.parashift.com/c%2B%2B-faq/cpp-objs-passed-to-c.html The answer is in two parts. First part I guess is till the
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is
From http://www.jibbering.com/faq/faq_notes/closures.html : Note: ECMAScript defines an internal [[prototype]] property of the internal Object
Based on What does volatile do? http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#incorrectlySync and New guarantees for volatile http://www.ibm.com/developerworks/library/j-jtp03304/ class
I'm adding a unit converter to my website from this link: http://www.unitconversion.org/faq.html And as

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.