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); } /*------------------------------------------------------------------------------*/
Hello.
// something wrong, when casting m.lArg0. lResults[0]=((uint32)lPtr) + ((uint32)(m.lArg0)); result = 1BBA6 // ok; result = DDDB lResults[1]=((uint32)lPtr); lResults[1] += ((uint32)(m.lArg0));
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));
……. uint8 XDATA * DATA RAMDISK; ……. /* write to file */ void rdisk_fwrite(file_entry XDATA *mfile, uint8 XDATA *buff, uint16 len) { uint16 DATA i; #ifdef __SELECT_ADAPTER_CONFIG uint8 XDATA * DATA ptr; ptr=&RAMDISK[mfile->file_start_ram_index+mfile->file_len]; i=len; while (i--) *ptr++ = *buff++; #else # ifdef __SELECT_TERMINAL_CONFIG MEMORY_UINT DATA ptr; uint8 DATA lByte; /* >>>>>>>>>>>>>>>>>>>>>here the simulator reported strange result in 'ptr' variable */ ptr=((MEMORY_UINT)RAMDISK)+mfile->file_start_ram_index+(MEMORY_UINT)mfile->file_len; /* >>>>>>>>>>>>>>>>>>>>>here the simulator reported strange result in 'ptr' variable */ i=len; while (i--) { lByte=*buff++; RAMDISK_put(ptr, lByte); ptr++; } # endif #endif buff-=len; update_crc(buff, len, &mfile->file_csum); mfile->file_len+=len; }
Sorry, but C51 Version 7.08 compiles the code OK. Maybe you have some special compiler settings or so, but the code prints OK. It seems that the problem has been solved already in Version 7.04 in context with the "integer promotion problems with combined pointer and char arithmetic". Refer also to the Release Notes.