Connecting Tech Pros Worldwide Forums | Help | Site Map

delete operator

bob@coolgroups.com
Guest
 
Posts: n/a
#1: Mar 18 '06

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?




Heinz Ozwirk
Guest
 
Posts: n/a
#2: Mar 18 '06

re: delete operator


<bob@coolgroups.com> schrieb im Newsbeitrag news:1142666617.644874.119810@p10g2000cwp.googlegr oups.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]

No! No!

Heinz
Jim Langston
Guest
 
Posts: n/a
#3: Mar 18 '06

re: delete operator


<bob@coolgroups.com> wrote in message
news:1142666617.644874.119810@p10g2000cwp.googlegr oups.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?


Tomás
Guest
 
Posts: n/a
#4: Mar 18 '06

re: delete operator


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_cast< 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]

rather than an expression of type: int *

Which is why I'd write:

delete &array[0];


-Tomás
Rolf Magnus
Guest
 
Posts: n/a
#5: Mar 18 '06

re: delete operator


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.

Rolf Magnus
Guest
 
Posts: n/a
#6: Mar 18 '06

re: delete operator


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_cast< int(*)[5] >( new int[5] );[/color]

Well, the result of a reinterpret_cast 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]

That would have to be:

delete [] &array[0];

Peter_Julian
Guest
 
Posts: n/a
#7: Mar 18 '06

re: delete operator



<bob@coolgroups.com> wrote in message
news:1142666617.644874.119810@p10g2000cwp.googlegr oups.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.

Rocket* rocket = new Rocket;
void* voidptr = reinterpret_cast< void* >(rocket);
....
Rocket* rocketptr = reinterpret_cast< Rocket* >(voidptr);
delete rocketptr;

Tomás
Guest
 
Posts: n/a
#8: Mar 18 '06

re: delete operator


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_cast< int(*)[5] >( new int[5] );[/color]
>
> Well, the result of a reinterpret_cast 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]


Again you're right.


-Tomás

red floyd
Guest
 
Posts: n/a
#9: Mar 18 '06

re: delete operator


Peter_Julian wrote:
[color=blue]
> Rocket* rocket = new Rocket;
> void* voidptr = reinterpret_cast< void* >(rocket);
> ...
> Rocket* rocketptr = reinterpret_cast< Rocket* >(voidptr);
> delete rocketptr;
>[/color]

Use static_cast<T*> to go back and forth between void* and T*.
Peter_Julian
Guest
 
Posts: n/a
#10: Mar 19 '06

re: delete operator



"red floyd" <no.spam@here.dude> wrote in message
news:Yc%Sf.48375$F_3.19716@newssvr29.news.prodigy. net...
| Peter_Julian wrote:
|
| > Rocket* rocket = new Rocket;
| > void* voidptr = reinterpret_cast< void* >(rocket);
| > ...
| > Rocket* rocketptr = reinterpret_cast< 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_cast<>, 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
reinterpretation with no bit pattern modification allowed, a
reinterpret_cast<> is a much better description of the transformation taking
place.

Closed Thread