Thread: how to delete void* pointer

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    how to delete void* pointer

    Hello,
    is it possible to delete a void pointer, without explicitly typecasting it? I know the size of the memory to free, but not the type of the pointer. Example with typecasting:

    Code:
    int main(){
      void* ptr;
      ptr = new int(7);
      delete static_cast<int>(ptr);
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you don't know the type, you're screwed. There is no valid way of freeing a pointer except through its original type (or a base class if you have a virtual destructor).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Thanks for your answer, although it is not possible.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I know the size of the memory to free, but not the type of the pointer.
    There's more to delete than freeing memory, and the type of the pointer has to be known. If you're only going to be working with raw memory, you might consider using malloc and free instead of new and delete:
    Code:
    #include <cstdlib>
    
    int main()
    {
      void *ptr = std::malloc ( 7 * sizeof ( int ) );
      
      //...
    
      std::free ( ptr );
    }
    My best code is written with the delete key.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm 99% sure that you can delete any pointer [although in this case, you should use delete [], as you create an array in the new int[7] call].

    The size of the object allocated is held internally in the heap, so there should be on problem with that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm 99% sure that you can delete any pointer
    I'm 100% sure that if the static type of the pointer being deleted is void, you've invoked undefined behavior.
    My best code is written with the delete key.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe a better question is, why are you creating a void* pointer to begin with instead of specifying the actual type?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh sure, you can delete a void* pointer and on MSVC it generally works fine so long as it's raw memory or built-in types. But when you start deleting void* pointers to classes, you get problems.
    I'm kind of betting it's undefined behavior. But in C++ this is very easy to avoid - simple use templates!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh sure, you can delete a void* pointer and on MSVC it generally works fine so long as it's raw memory or built-in types. But when you start deleting void* pointers to classes, you get problems.
    From the 2003 edition of the C++ Standard, section 5.3.5:
    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. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.
    The footnote to the above paragraph reads:
    This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void.
    I'm kind of betting it's undefined behavior.
    You get nothing from winning the bet since Prelude already stated she was 100&#37; sure of it... not believing Prelude in such things is just plain stupid
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I feel like throwing a temper tantrum, because I said it first: "no valid way"
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Wouldn't it be possible to call ::operator delete(), assuming that the memory is POD, or has been properly destroyed before hand?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    Wouldn't it be possible to call :perator delete(), assuming that the memory is POD, or has been properly destroyed before hand?
    That would be the same thing as calling delete or delete [] - that is what it does "behind the scenes".

    Given that it's "undefined", I would agree with the "use malloc/free" advice given above. Or just use a non-void pointer in the first place - it is really very rare that there is a valid reason to create a void * - you may convert something to a void pointer later, but you need to convert the void pointer to something else ANYWAYS to use it for anything [other than generic memory functions, reading/writing to files etc of course].

    This is another case where the original poster probably needs to provide a bit more info about what the actual problem - this is a typical "I need help taking the wheelnuts out", when the actual problem is a punctured tyre [and the "right" solution may even be to squirt in some "gunk" to seal and inflate the tyre, rather than put on a spare wheel].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by matsp View Post
    That would be the same thing as calling delete or delete [] - that is what it does "behind the scenes".
    Except that ::operator delete takes a void pointer (at least when it's overloaded). A call to ::operator delete() does not pass any information about the type of the pointer to the operation. So the fact that you cannot call "delete voidPtr" does not preclude calling "::delete(voidPtr). Indeed, that often what a class specific overload of delete would do.

    Furthermore, operator delete can called outside of a delete call in an implementation of an STL compatible allocator. That allocator again does not pass any type information to ::operator delete().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You have to deallocate with the operation that matches the allocation operation. When you allocate with new, you have to deallocate with delete. You have to match new[] with delete[]. You have to match malloc() with free(). And you have to match operator new with operator delete. Everything else is always undefined behaviour.

    In other words: if using ::operator delete() on the void* works on your platform for PODs, delete or delete[] will probably work just as well. Doesn't change the fact that both are undefined behaviour.

    The allocator objects used in the STL still follow this guideline. std::allocator uses ::operator new and ::operator delete to allocate and deallocate memory, then uses placement new and explicit destructor calls for construction and destruction.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In any case, if you have a void* pointer and you don't know what type it is, you either shouldn't be the one deleting it, or you should have kept track of the original type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  3. delete pointer inside ?
    By black in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2004, 12:19 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM