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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:38:50+00:00 2026-06-12T16:38:50+00:00

Possible Duplicate: Is it safe to delete a void pointer? Say I have a

  • 0

Possible Duplicate:
Is it safe to delete a void pointer?

Say I have a new allocation to a class called MyClass and allocation is as simple as:

MyClass *myClassPtr = new MyClass();

And I store reference to the list of void* where I simply say

myListOfPointers.add(static_cast<void*>(myClassPtr)); // this has to be void*

And later I release memory so instead of doing:

delete myClassPtr

I use:

delete MyListOfPointer.get(0)

(Say myClassPtr reference is at zero-index.) Also, please note that it has to be void* since this list can store different types of pointers so I wouldn’t know the type of pointer that I am deleting:

So I can’t do any thing like:

delete static_cast<MyClass*>(MyListOfPointer.get(0))

Is this way going to release the correct memory size? (sizeof(MyClass))?

Note:
I am not looking for any answer pointing to smart pointers.

  • 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-12T16:38:51+00:00Added an answer on June 12, 2026 at 4:38 pm

    Deleting through a void* results in undefined behavior, so you are guaranteed nothing.

    5.3.5 Delete [expr.delete]

    1 The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression.
    […]
    The operand shall have a pointer to object type, or a class type having a single non-explicit conversion function (12.3.2) to a pointer to object type. The result has type void.78

    78) This implies that an object cannot be deleted using a pointer of type void* because void is not an object type.

    Emphasis mine.


    So even though you said not to say it, the answer is to create some form of smart pointer. It would need to use type-erasure to hide the type externally (allowing the heterogeneous list), but internally keep track of the type it was given and how to delete it. Something much like boost::any.

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

Sidebar

Related Questions

Possible Duplicate: delete[] supplied a modified new-ed pointer. Undefined Behaviour? Let's say I've allocated
Possible Duplicate: Is it safe to delete a void pointer? Will the following code
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
Possible Duplicate: Are C# arrays thread safe? I have been programming C# for 10
Possible Duplicate: Are C# arrays thread safe? I have a program that is single-threaded
Possible Duplicate: stdout thread-safe in C on Linux? Say thread1 and thread2 are similar
Possible Duplicate: Is the C# static constructor thread safe? I have multiple threads using
Possible Duplicate: How thread-safe is enum in java? Let there be an enum class
Possible Duplicate: Smart pointers/safe memory management for C? I have an embedded application where
Possible Duplicate: Is it safe to assume strict comparison in a Javascript switch statement?

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.