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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:57:12+00:00 2026-06-06T07:57:12+00:00

Why does the following program crash? I have a base class whose destructor is

  • 0

Why does the following program crash? I have a base class whose destructor is not virtual but the child class destructor is virtual:

#include <iostream>

class Base {
public:
  Base() {
    std::cout << "Base::Base CTOR " << std::endl;
  }
  ~Base() {
    std::cout << "Base::Base DTOR " << std::endl;
  }
};

class Child : public Base {
public:
  Child(){
    std::cout << "Child::Child CTOR " << std::endl;
  }
  virtual ~Child() {
    std::cout << "Child::Child DTOR " << std::endl;
  }

};

int main (int argc, char **argv) {
  Base *ptr = new Child;
  delete ptr;
}
  • 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-06T07:57:13+00:00Added an answer on June 6, 2026 at 7:57 am

    What you are observing is called “undefined behavior”. Make Base‘s dtor virtual if you want do call delete on Child instance through Base pointer.

    From the 2003 standard, 5.3.5/3:

    In the first alternative (delete object), if the static type of the
    operand is different from its dynamic type, the static type shall be a
    base class of the operand’s dynamic type and the static type shall
    have a virtual destructor or the behavior is undefined.

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

Sidebar

Related Questions

For some reason the following doesn't crash like my program does, but I'm pretty
Does anyone have any idea why the following causes the program to crash? NSFileManager
The following program does not work as I intended. #include <string.h> #include <stdio.h> int
I have the following program does not work NOTE: compiled on windows 7. Gdk.Screen
When answering another question, I realized that the following program does not quite do
I have a file transfer program. The program (Client) does following operations to send
The following program does not work as expected: module Main where import Graphics.UI.Gtk import
After writing the following program, it does not appear to pass arguments to the
consider the following program: namespace NS2 { class base { }; template<typename T> int
The following program compiles with ifort (version 12) but not with GFortran (up to

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.