Hello the following program does not work in gcc 4.1.1 compiler //tried to analyse it and found vptr is laid out in front and so its shifting 4 bytes and data gets c
#include<iostream> #include<stdio.h> using namespace std; typedef struct c { int a; int b; char bb[0]; }; class d: public c { public: virtual ~d() {;} string x; short y; }d1; main() { c c1; // c object c1.a=5; c1.b=6; d1.x="abc"; //d object d1.y=4; d *ptr = reinterpret_cast<d*>(&c1); cout<<ptr->a<<endl<<ptr->b<<endl; } <*************************************************> Compile g++ test.cpp OUTPUT: 6 -1074749676 **************************************************** wheras the expected answer is 5 and 6. **************************************************** where is the vptr laid out in binary layout of object? and why it skips 4 bytes always. -- Summary: reintrepret_cast corrupts data / does not work as expected Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: shyam_77_2000 at yahoo dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36964