This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/15942] modifying data in a int * cast pointer to a pointer to a member has no effect when -O2 is used


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]