Mail Archives: djgpp/2000/09/17/20:30:32
On Mon, 18 Sep 2000 00:00:14 +0200, "Rafał Maj" <r_maj AT poczta DOT onet DOT pl>
wrote in comp.os.msdos.djgpp:
> Hi,
> is this code correct :
>
> int *pos = new int[2];
> void* var = (void*)pos;
> delete []((int*)var);
>
> I'm not sure, will everything be o.k. when I'll temporarly store adress of
> int[] array in void*
> If this example has bug, how to fix it ?
>
> Thanks,
> Rafal
This is perfectly OK as long as you use the proper type of pointer
when you use delete or delete[] on it.
Both C and C++ guarantee that:
1. You can store a pointer to any object type in a pointer to void
(requires a cast in C++, not in C).
2. Assign or cast that pointer value back to a pointer to the
original type.
3. The final result will point to the same object.
So assigning pos to a pointer to void, and casting that pointer to
void back to pointer to int produces a pointer to int that still
points to the first of the two ints allocated to pos.
Jack Klein
--
Home: http://jackklein.home.att.net
- Raw text -