🔒 Locked
using delete on unknown types
Advertisement
Original Post
I''m writing a linked list class, which holds a void pointer to its data item:
struct node
{
node * next, * prev;
void * data;
};
Anyways, when I create the node I assign whatever my object is to data, and when I want to use it I just cast it back to its old type. I''m wondernig about the destructor for the linked list class: can I safely delete data? I mean, in this context, data is a void pointer, so will delete know how much memory to free? And doesn''t delete call the destructor of whatever it''s called on?
If I can''t do that, then I guess I have to write a loop to cast all my data and free it before I delete the entire list.
Thanks!
quote:
Original post by _SHO_NUFF
I mean, in this context, data is a void pointer, so will delete know how much memory to free?
Yes.
quote:
Original post by _SHO_NUFF
And doesn''t delete call the destructor of whatever it''s called on?
Not in this case. It doesn''t know what type it''s deleting. So, in summary: it''s not safe to do that in C++ (in C it could be prefectly acceptable since there are never destructors).
quote:
Original post by _SHO_NUFF
If I can''t do that, then I guess I have to write a loop to cast all my data and free it before I delete the entire list.
No, just use templates, that''s what they''re for.

quote:
Original post by _SHO_NUFF
But do people really use templates for this sort of thing? ugly
Yes. My three linked list types do, the STL''s do, et cetera.
quote:
Original post by _SHO_NUFF
What if I tried something fancy, like attaching a pointer to the destructor of data?
And you think templates are ugly?

quote:
Original post by _SHO_NUFF
it''s things like this that make me appreciate Java.
Which doesn''t have generic programming at all (yet, at least)

quote:
Original post by _SHO_NUFF
it''s things like this that make me appreciate Java.
Java only does what you could do in C++ - you derive everything from a common base and call a virtual destructor. C++ templates allow the same functionality but without the (admittedly tiny) runtime CPU and memory expense of virtual functions. C++ gives you the choice!
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
you should be using a base class with virtual destructor if your dont like templates (i dont, thats why i use polymorphism). there should be no need ever to recast your data nor use a void* in the way you are when using c++ (there are some expcetions). the data is either a block of memory in which there is no destructor needed, in which case void* is fine (though i suggest malloc/free when dealing with void*). if its an array of objects, then you should be using a base class and virtual destructors (or templates).
delete will know how much memory to free, no matter what the data type (internal the amount of memory allocated is stored in a table, so the OS knows how much was allocated). just make sure that your dont have any wierd dependencies that may cause you to delete things twice or delete something that another part of the code needs.
nothing fancy is really needed, thats what polymorphism is for.
delete will know how much memory to free, no matter what the data type (internal the amount of memory allocated is stored in a table, so the OS knows how much was allocated). just make sure that your dont have any wierd dependencies that may cause you to delete things twice or delete something that another part of the code needs.
nothing fancy is really needed, thats what polymorphism is for.
Why don't you just use list from the STL. Then you wont have to worry at all.
[edited by - Bloodshot Eye on April 15, 2002 1:39:15 AM]
[edited by - Bloodshot Eye on April 15, 2002 1:39:15 AM]
Hey, great idea, use the linked list from the STL. That way, I wouldn''t have to learn anything.
So, A Person, are you saying that instead of void* I should have a pointer of the type of my base object? And that in my Object class, all I have to do is declare
virtual object::~object();
and it will be safe to call delete on any object or derived class? Thanks for the great help.
So, A Person, are you saying that instead of void* I should have a pointer of the type of my base object? And that in my Object class, all I have to do is declare
virtual object::~object();
and it will be safe to call delete on any object or derived class? Thanks for the great help.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement