This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/15942] modifying data in a int * cast pointer to a pointer to a member has no effect when -O2 is used
- From: "bangerth at dealii dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 11 Jun 2004 17:19:48 -0000
- Subject: [Bug c++/15942] modifying data in a int * cast pointer to a pointer to a member has no effect when -O2 is used
- References: <20040611161330.15942.indrek@mare.ee>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From bangerth at dealii dot org 2004-06-11 17:19 -------
Oh, and as for the compiler allowing to compile this code: you keep
using old-style (type)value casts. The language standard says that the
compiler will treat that is if you knew exactly what you did, just as
in reinterpret_cast. Apparently, you don't, and in this case static_cast
would be the better choice. "Fixing" your code in this respect like so
-----------------
#include <stdio.h>
struct a
{
int x[8];
};
int main ()
{
int a:: *n[8];
for ( int j = 0; j < 8; j++ ) {
n[j] = static_cast<int a::*>(&a::x);
*static_cast<int *>(&n[j]) += j*8;
}
printf ("And the results are:\n");
for ( int j = 0; j < 8; j++ ) {
printf ("%x\n", static_cast<int>(n[j]));
}
return 0;
}
-------------------------
yields this interesting assortment of errors:
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -W -Wall
x.cc: In function `int main()':
x.cc:13: error: invalid static_cast from type `int a::*)[8]' to type `int
a::*'
x.cc:15: error: invalid static_cast from type `int a::**' to type `int*'
x.cc:20: error: invalid static_cast from type `int a::*' to type `int'
This is a clear indication that the code is wrong in at least 3 places.
W.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15942