Thread: Deleting void pointer

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Deleting void pointer

    Hi,

    I have a following program:

    Code:
    #include <iostream>
    using namespace std;
    class Object {
       void* data; // Some storage
       const int size;
       const char id;
    public:
       Object(int sz, char c) : size(sz), id(c) {
         data = new char[size];
         cout << "Constructing object " << id
              << ", size = " << size << endl;
       }
       ~Object() {
         cout << "Destructing object " << id << endl;
         delete []data; 
       }
    };
    int main() {
       Object* a = new Object(40, 'a');
       delete a;
       void* b = new Object(40, 'b');
       delete b;
    }
    I just want to know that on deleting pointer b what will happen ????

    This program is running not calling b destructor. Then what is deleted by "delete b " ???

    Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Pay attention to your warnings:
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Ya...... I know that............
    But I want to know that what b is deleting ?????
    Surely it is not freeing the m/m since destructor is not called .....

    Thanks

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bargi View Post
    Ya...... I know that............
    But I want to know that what b is deleting ?????
    Surely it is not freeing the m/m since destructor is not called .....

    Thanks
    It would free the memory itself.

    Just like:
    Code:
    char *p = new char [20];
    ... 
    delete p;
    Since your void * doesn't have a destructor, there is no call to the destructor.

    --
    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.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it's undefined as the warning says, how can you be sure that it indeed frees the memory?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    If it's undefined as the warning says, how can you be sure that it indeed frees the memory?
    Well, to be technical about it, I can't be SURE. But I do expect delete to actually free the memory related to the memory allocation.

    However, there's very rarely a good need to use void pointers to point to objects, and not being able to run the destructor is of course a hugely bad thing.

    --
    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.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Bargi View Post
    Hi,

    I have a following program:

    Code:
    #include <iostream>
    using namespace std;
    class Object {
       void* data; // Some storage
       const int size;
       const char id;
    public:
       Object(int sz, char c) : size(sz), id(c) {
         data = new char[size];
         cout << "Constructing object " << id
              << ", size = " << size << endl;
       }
       ~Object() {
         cout << "Destructing object " << id << endl;
         delete []data; 
       }
    };
    int main() {
       Object* a = new Object(40, 'a');
       delete a;
       void* b = new Object(40, 'b');
       delete b;
    }
    I just want to know that on deleting pointer b what will happen ????

    This program is running not calling b destructor. Then what is deleted by "delete b " ???

    Thanks
    Is there any particular reason why you're using a void* instead of a char* ?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, as we would have it, operator delete takes a void* (although I'm not sure if the standard says this).
    Therefore, technically it would be able to free the memory of the object since it is allocated by operator new.
    The only thing missing is the destructor.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Well, as we would have it, operator delete takes a void* (although I'm not sure if the standard says this).
    Therefore, technically it would be able to free the memory of the object since it is allocated by operator new.
    The only thing missing is the destructor.
    I believe the standard says that operator ::delete() would take a void pointer. It is the keyword delete, not operator function delete that causes the call to the destructor - by this, I meant hat the compiler generates code that essentially does this:
    Code:
    T *p = new T;
    ... 
    delete p;
    // becomes:
    p->~T();
    ::delete(p);
    The blue part is not code that we see in the source, but what the compiler actually generates when it sees the blue delete p;

    Of course, if T is not a class type, then T doesn't have a ~T(), so there's no code for the compiler to call to delete the object. I had the same question as cpjust on my mind when I started reading this thread, and it's still unanswered.

    --
    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.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    I had the same question as cpjust on my mind when I started reading this thread, and it's still unanswered.
    Well, reason for posting is that I want to know that whether the memory is freed or there is a m/m leak.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The behavior is undefined.
    Never call delete on a void pointer.
    Instead, if you need to work with generic types, 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.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can use ::delete(ptr) to free the memory pointed to by a void pointer. However, unless the value pointed to is a POD type, a primitive, or has been previously destroyed, there is likely to be a problem resulting from an object contained in your object using dynamic memory or other resource.
    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.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you can't. That's still undefined behaviour.

    You can use ::operator delete(ptr) to just call the memory deallocation function.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM