Nonplussed 0 Posted March 18, 2011 Report Share Posted March 18, 2011 The class member (pMember) returns unexpected values when referenced. #include <system.h> char* pGlobal = 0x500; class CTest { public: CTest() { char* pReceipient = 0; char* pLocal = 0x500; pReceipient = (char*) &pGlobal[1]; // pReceipient = 0x501 pReceipient = (char*) &pLocal[1]; // pReceipient = 0x501 pReceipient = (char*) &pMember[1]; // pReceipient = Different values depending on size of build char* pDummy = pReceipient; }; private: char* pMember = 0x500; }; void main() { CTest Test; } Quote Link to post Share on other sites
rbairos 0 Posted April 14, 2011 Report Share Posted April 14, 2011 Wow, I just spent a week pinpointing a similar bug. Here it is, if it sheds any light. In a nutshell, all four methods are overwriting the first byte of the array, instead of writing into first, second, third, fourth. class foo { public: char *first() { return (char *)&data[0]; } char *second() { return (char *)&data[1]; } char *third() { return (char *)&data[2]; } char *fourth() { return (char *)&data[3]; } char *getData() { return data; } private: char data[4]; }; void main() { class foo f; *f.first() = 1; // stuffs data[0] *f.second() = 2; // stuffs data[0], not data[1] *f.third() = 3; // stuffs data[0], not data[2] *f.fourth() = 4; // stuffs data[0], not data[3] char *data = f.getData(); char a = data[0]; // returns 4, instead of 1 char b = data[1]; // returns 0, instead of 2 char c = data[2]; // returns 0, instead of 3 char d = data[3]; // returns 0, instead of 4 } Incidentally, if I change the function signature to: char *first() { return &data[0]; } char *second() { return &data[1]; } char *third() { return &data[2]; } char *fourth() { return &data[3]; } I get a compiler errors: mainloop.c(12): error: can't convert 'class foo' to 'unsigned char*' mainloop.c(12:18): error: failed to generate expression mainloop.c(13): error: can't convert 'class foo' to 'unsigned char*' mainloop.c(13:19): error: failed to generate expression mainloop.c(14): error: can't convert 'class foo' to 'unsigned char*' mainloop.c(14:18): error: failed to generate expression mainloop.c(15): error: can't convert 'class foo' to 'unsigned char*' mainloop.c(15:19): error: failed to generate expression Why its treating member 'data' as 'class foo' is also strange and may be a clue. Any help appreciated. Thanks, Rob. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.