<bob@coolgroups .com> schrieb im Newsbeitrag news:1142666617 .644874.119810@ p10g2000cwp.goo glegroups.com.. .[color=blue]
>
> Let's say you use the delete operator as follows:
>
> Rocket *rocket = new Rocket;
> void *voidptr = (void *) rocket;
> delete voidptr;
>
> Does the memory get deleted right? Do delete operations on void
> pointers generally free the memory right?[/color]
<bob@coolgroups .com> wrote in message
news:1142666617 .644874.119810@ p10g2000cwp.goo glegroups.com.. .[color=blue]
>
> Let's say you use the delete operator as follows:
>
> Rocket *rocket = new Rocket;
> void *voidptr = (void *) rocket;
> delete voidptr;
>
> Does the memory get deleted right? Do delete operations on void
> pointers generally free the memory right?[/color]
How does the compiler know what destructor to call on a void* since it
doesn't know it's type?
posted:
[color=blue]
>
> Let's say you use the delete operator as follows:
>
> Rocket *rocket = new Rocket;
> void *voidptr = (void *) rocket;
> delete voidptr;
>
> Does the memory get deleted right? Do delete operations on void
> pointers generally free the memory right?
>[/color]
You should supply the "delete" operator with an expression which has the
_exact_ same time as the "new" expression which created it. So don't give
it anything other than a "Rocket*".
Here's an even more subtle example:
int (&array)[5] = *reinterpret_ca st< int(*)[5] >( new int[5] );
When we want to delete it, if we did the following:
delete &array;
Then we would be giving it an expression of type: int (*)[5]
bob@coolgroups. com wrote:
[color=blue]
>
> Let's say you use the delete operator as follows:
>
> Rocket *rocket = new Rocket;
> void *voidptr = (void *) rocket;
> delete voidptr;
>
> Does the memory get deleted right?[/color]
No.
[color=blue]
> Do delete operations on void pointers generally free the memory right?[/color]
No.
Providing a void* to delete results in undefined behavior.
Tomás wrote:
[color=blue]
> posted:
>[color=green]
>>
>> Let's say you use the delete operator as follows:
>>
>> Rocket *rocket = new Rocket;
>> void *voidptr = (void *) rocket;
>> delete voidptr;
>>
>> Does the memory get deleted right? Do delete operations on void
>> pointers generally free the memory right?
>>[/color]
>
> You should supply the "delete" operator with an expression which has the
> _exact_ same time as the "new" expression which created it.[/color]
That's not right. If Rocket is derived from a class with a virtual
destructor, you can also delete the object through a pointer to that type.
[color=blue]
> So don't give it anything other than a "Rocket*".
>
> Here's an even more subtle example:
>
> int (&array)[5] = *reinterpret_ca st< int(*)[5] >( new int[5] );[/color]
Well, the result of a reinterpret_cas t is implementation-defined, so it
might or might not do what you expect it to.
[color=blue]
> When we want to delete it, if we did the following:
>
> delete &array;
>
> Then we would be giving it an expression of type: int (*)[5]
>
> rather than an expression of type: int *
>
> Which is why I'd write:
>
> delete &array[0];[/color]
<bob@coolgroups .com> wrote in message
news:1142666617 .644874.119810@ p10g2000cwp.goo glegroups.com.. .
|
| Let's say you use the delete operator as follows:
|
| Rocket *rocket = new Rocket;
| void *voidptr = (void *) rocket;
| delete voidptr;
|
| Does the memory get deleted right? Do delete operations on void
| pointers generally free the memory right?
|
Thats guarenteed to fail. Which is why C++ has provided you with a safe
casting mechanism.
Rolf Magnus posted:
[color=blue]
> Tomás wrote:
>[color=green]
>> posted:
>>[color=darkred]
>>>
>>> Let's say you use the delete operator as follows:
>>>
>>> Rocket *rocket = new Rocket;
>>> void *voidptr = (void *) rocket;
>>> delete voidptr;
>>>
>>> Does the memory get deleted right? Do delete operations on void
>>> pointers generally free the memory right?
>>>[/color]
>>
>> You should supply the "delete" operator with an expression which has
>> the _exact_ same time as the "new" expression which created it.[/color]
>
> That's not right. If Rocket is derived from a class with a virtual
> destructor, you can also delete the object through a pointer to that
> type.[/color]
You're right.
[color=blue][color=green]
>> So don't give it anything other than a "Rocket*".
>>
>> Here's an even more subtle example:
>>
>> int (&array)[5] = *reinterpret_ca st< int(*)[5] >( new int[5] );[/color]
>
> Well, the result of a reinterpret_cas t is implementation-defined, so it
> might or might not do what you expect it to.[/color]
I started a thread elsewhere about this...
[color=blue][color=green]
>> When we want to delete it, if we did the following:
>>
>> delete &array;
>>
>> Then we would be giving it an expression of type: int (*)[5]
>>
>> rather than an expression of type: int *
>>
>> Which is why I'd write:
>>
>> delete &array[0];[/color]
>
> That would have to be:
>
> delete [] &array[0];[/color]
"red floyd" <no.spam@here.d ude> wrote in message
news:Yc%Sf.4837 5$F_3.19716@new ssvr29.news.pro digy.net...
| Peter_Julian wrote:
|
| > Rocket* rocket = new Rocket;
| > void* voidptr = reinterpret_cas t< void* >(rocket);
| > ...
| > Rocket* rocketptr = reinterpret_cas t< Rocket* >(voidptr);
| > delete rocketptr;
| >
|
| Use static_cast<T*> to go back and forth between void* and T*.
There are situations where using static_cast<> presents a special challenge.
Unlike a reinterpret_cas t<>, the former is allowed to modify the bit pattern
of the result. A static_cast<> is a more dangerous type of cast.
In most cases, using a static_cast<> between void* and T* is safe enough.
But for the sake of purpose, and since what you are actually doing is a
reinterpretatio n with no bit pattern modification allowed, a
reinterpret_cas t<> is a much better description of the transformation taking
place.
Comment