Archived

This topic is now archived and is closed to further replies.

denethor

void pointer

Recommended Posts

Does a void pointer have to be casted before delete is used? For example: void *p; (double*)(*p) = new double; then should i put ''delete p;,'' or ''delete (double*)p;'' ?

Share this post


Link to post
Share on other sites
No, you don't need to cast. However,

(double *)(*p) = new double;

isn't what you want. It should be

p = (void *)new double;

But, why would you want to do that anyway?


[edited by - Dave Hunt on January 29, 2003 3:11:57 PM]

Share this post


Link to post
Share on other sites
Well I have a list, containing variables of different types, such as double* and char*. When I try to cycle through the list and delete each pointer, I get an error. I''m thinking that becaue the pointer are void, the program has no way of knowing how much memory to delete.

Share this post


Link to post
Share on other sites
I thought that you only needed a cast if you want the destructor to be called. Since he''s using built-in types, I would have thought it would be OK not to have to store extra info about the type so it can be cast back later. The allocation mechanism knows how much memory to delete after all.

You could also allocate the built-in types with malloc() and free them up with free(), which takes a void* parameter anyway. Don''t use malloc for objects with constructors or virtual functions though unless you want to be eaten by bugs.

Share this post


Link to post
Share on other sites
quote:
Original post by Paradigm Shifter
I thought that you only needed a cast if you want the destructor to be called.

You need to give the delete operator the correct type if you want any sort of defined behaviour.
quote:

Since he''s using built-in types, I would have thought it would be OK not to have to store extra info about the type so it can be cast back later.

Every time you give-up on the C++ type-system by casting to void*, you have to create your own type-system that does what you want. This can be done, but it can get quite complicated, particularly if you''re under any illusions about what behaviour you can rely on in such a scenario.
quote:

The allocation mechanism knows how much memory to delete after all.

Don''t make excuses. You have to cast to the correct type, else you have undefined behaviour, which means C++ cannot guarantee what happens.

Share this post


Link to post
Share on other sites
Fair enough. Thinking back to my C void* days, we always had a type field at a known position in the various struct types stuffed in the array so when we got things out of an array of void*''s we could use it sensibly. An array of void*''s pointing to char arrays and doubles is useless after all unless you know the type, you need to cast the pointer back when you use it.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites