void pointer

Started by
7 comments, last by denethor 21 years, 2 months ago
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;'' ?
Advertisement
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]
Shouldn't it just be
void *p;p = (void *)new double; 
?

[edited by - Extrarius on January 29, 2003 3:12:26 PM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Yes. I was correcting myself as you were posting
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.
quote:Original post by denethor
Does a void pointer have to be casted before delete is used?

Yes. To the correct type.
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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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.
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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement