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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:37:28+00:00 2026-05-24T21:37:28+00:00

Assume you have an object of class Fool . class Fool { int a,b,c;

  • 0

Assume you have an object of class Fool.

class Fool
{
    int a,b,c;
    double* array ;
    //...
    ~Fool()
    {
        // destroys the array..
        delete[] array ;
    }
};


Fool *fool = new Fool() ;

Now, I know you shouldn’t, but some fool calls the destructor on fool anyway. fool->~Fool();.

Does that mean fool‘s memory is freed, (ie a,b,c are invalid) or does that mean only whatever deallocations in ~Fool() function occur (ie the array is deleted only?)

So I guess my question is, is a destructor just another function that gets called when delete is called on an object, or does it do more?

  • 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-24T21:37:29+00:00Added an answer on May 24, 2026 at 9:37 pm

    If you write

    fool->~Fool();
    

    You end the object’s lifetime, which invokes the destructor and reclaims the inner array array. The memory holding the object is not freed, however, which means that if you want to bring the object back to life using placement new:

    new (fool) Fool;
    

    you can do so.

    According to the spec, reading or writing the values of the fields of fool after you explicitly invoke the destructor results in undefined behavior because the object’s lifetime has ended, but the memory holding the object should still be allocated and you will need to free it by invoking operator delete:

    fool->~Fool();
    operator delete(fool);
    

    The reason to use operator delete instead of just writing

    delete fool;
    

    is that the latter has undefined behavior, because fool‘s lifetime has already ended. Using the raw deallocation routine operator delete ensures that the memory is reclaimed without trying to do anything to end the object’s lifetime.

    Of course, if the memory for the object didn’t come from new (perhaps it’s stack-allocated, or perhaps you’re using a custom allocator), then you shouldn’t use operator delete to free it. If you did, you’d end up with undefined behavior (again!). This seems to be a recurring theme in this question. 🙂

    Hope this helps!

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

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
Assume that I have a particular class of object that defines a class method
Assume I have an enumerable object enum and now I want to get the
Assume that I have a class value object defined in php, where each variable
Assume I have a class called MyClass that has two properties (int Id and
Lets assume we have a class in python: class A(object): def __del__(self): print Del!
Assume that we have an object k of type class A . We defined
Assume I have a class called Customer . Now I need to render the
Assume I have a business object like this, class Employee { public string name;
Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>

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.