This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/29286] [4.0/4.1/4.2/4.3 Regression] placement new does not change the dynamic type as it should
- From: "pinskia at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 May 2007 17:55:32 -0000
- Subject: [Bug libstdc++/29286] [4.0/4.1/4.2/4.3 Regression] placement new does not change the dynamic type as it should
- References: <bug-29286-10053@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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