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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:30:26+00:00 2026-06-02T13:30:26+00:00

Is it possible to call the destructor of an object without knowing the class

  • 0

Is it possible to call the destructor of an object without knowing the class type without using delete? I am asking because I am working on an allocator (for fun/ practice) and I am using malloc/ placement new to constructor the object but then when I go to destruct the object, I was curious if there was a way to do so without knowing the type. If it is not possible, why not? Is it the only way to do is the way I show in my sample code (that is commented out)?

#include <stdio.h>
#include <new>

void* SomeAllocationFunction(size_t size) {
    return malloc(size);
}

class SomeClass{
public:
    SomeClass() {
        printf("Constructed\n");
    }

    ~SomeClass() {
        printf("Destructed\n");
    }
};

int main(void){
    void* mem = SomeAllocationFunction(sizeof(SomeClass));
    SomeClass* t = new(mem)SomeClass;

    free(t);
    //t->~SomeClass(); // This will call the destructor, is it possible to do this without knowing the class?

    return 0;
}

(I know I can just call delete, but please ignore that for the moment.)

  • 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-02T13:30:28+00:00Added an answer on June 2, 2026 at 1:30 pm

    No, it’s not possible without knowing the type (or knowing one of the object’s base types that has a virtual destructor).

    Typically speaking, custom allocators neither construct nor destruct the object, though some make a templated wrapper around the allocator that performs a placement new or a direct destructor call.

    (Technically, you could associate with every allocation a function pointer that ends up calling the type’s destructor. But that’s fairly sketchy, and I wouldn’t recommend it.)

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

Sidebar

Related Questions

Possible Duplicate: What is difference between instantiating an object using new vs. without This
Is it possible to call a web service from within Flash (using AS3)? Or
Is it possible to call functions from class like this: $class = new class;
Is it possible to call a class method of all objects in a list?
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Do I need to explicitly call the base virtual destructor? Hello all,
It's possible to call C functions through CREATE FUNCTION but how can .NET functions
Is it possible to call an api exposed in COM exe server from NSIS
Is it possible to call a c(++) static function pointer (not a delegate) like

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.