------- Comment #92 from pinskia at gcc dot gnu dot org  2007-05-18 18:55 
-------
> So if that is not valid, and the placement new case is valid, then what is the
> essential difference between the cases?  The variable is being accessed via 
> two
> different types.  Why is that OK?
> void f(double* p) { *(int*)p = 3; long *l = new (p) long; *l = 4; }
> void g() { int i; f((double *)&i); }

Because the memory that p was pointing to, stops being an int once a placement
new happens.  For F, it goes:
> void f(double* p)
> {
p points to an variable that is an int
> *(int*)p = 3
access the int via an int.
> long *l = new (p) long;
The memory is no longer an int, it has become a long, it cannot be accessed as
an int no longer as that would be undefined.
> *l = 4; 
Access the memory as a long and since the type is a long, this is well defined.
>}

Now after this function returns the variable can only be accessed as a long
(well and via a character type).

Hopefully this explains why this is valid.  Now should we do anything about it,
I don't know because how often does this happen in real life, I don't know.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29286

Reply via email to