We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Look at this program:
/*------------------------------------------------------------------------------*/ #include <reg51.h> #include <stdio.h> typedef unsigned long uint32; typedef unsigned short uint16; typedef unsigned char uint8; #define MEMORY xdata #define StructMEMORY xdata typedef struct { uint16 lArg0; uint32 lArg1; } MyStruct; uint8 xdata * MEMORY lPtr; MyStruct StructMEMORY m; void MyInit() { m.lArg0=8; m.lArg1=2; lPtr=(uint8 xdata *)0xddd3; } void bug_0(MyStruct StructMEMORY *p) { static uint32 MEMORY lResults[4]; lResults[0]=((uint32)lPtr) + p->lArg1 + (uint32)(p->lArg0); lResults[1]=p->lArg1 + (uint32)(p->lArg0) + ((uint32)lPtr); lResults[2]=((uint32)lPtr); lResults[2]+=p->lArg1 + (uint32)(p->lArg0); lResults[3]=(uint32)(lPtr+p->lArg1 + (uint32)(p->lArg0)); if (lResults[0]==lResults[1] && lResults[1]==lResults[2] && lResults[2]==lResults[3]) printf("\nNo problems."); else for(;/* BUG */;); } void main() { MyInit(); bug_0(&m); } /*------------------------------------------------------------------------------*/
You said it "works" in Borland. Don't forget that Borland (in fact, the PC) has a different byte ordering ("Endian-ness") from C51. It may also introduce padding bytes. Check the Manual for both compilers.
I don't understand what you want to say, but I know aboud big and small endian. the next code:
{ long a1,a2,b,c,d; b=14444;c=3;d=4; a1=b+c+d; a2=b; a2+=c+d; if (a1!=a2) printf("\ncompiler bug."); }
By the way, look at assembler listing, generated from my source code and you will find interesting thing, the place where compiler hungs and generates wrong assembler directives: multiplication of lPtr by 2, and no actions on calculation of structure fields.....
"the next code: { long a1,a2,b,c,d; b=14444;c=3;d=4; a1=b+c+d; a2=b; a2+=c+d; if (a1!=a2) printf("\ncompiler bug."); } must not print "compiler bug" at any C compiler, if, sure, that compiler works with 32 bit data." I compiled this using 7.01 and it works fine. I've no idea what your first code example above is supposed to achieve, but why do you think that casting pointers to unsigned long is a legitimate (or reasonable) thing to do anyway? Stefan