This code generate 2 bugs:
#define BUG_TEST 0 typedef char * PCHAR; typedef struct { int I; int J; } A_OLD; typedef struct { int nI; int nJ; } A, *PA; typedef A xdata * PAx; A_OLD cAOld; #if BUG_TEST == 0 //Normal int l1 = (PCHAR)&(*(PA)(&cAOld)); //Get address of struct int l2 = ((PCHAR)&((*(PA)(&cAOld)).nI) - (PCHAR)&(*(PA)(&cAOld))); //Calc offset of member #elif BUG_TEST == 1 //Cann't initialize int l1 = (PCHAR)&(*(PAx)(&cAOld)); //Get address of struct #elif BUG_TEST == 2 //General fault C51 int l2 = (unsigned int)((PCHAR)&((*(PA)(&cAOld)).nI) - (PCHAR)&(*(PA)(&cAOld)));//Calc offset of member #endif
Final report: -------------------------------------------- Compiler: --------------------------------------------
C51 COMPILER V7.04 DATA 03/15/2003 10:49:27 PAGE 1 C51 COMPILER V7.04, COMPILATION OF MODULE DATA OBJECT MODULE PLACED IN data.OBJ COMPILER INVOKED BY: D:\MCONTROL\KEIL704\C51\BIN\C51.EXE data.c OPTIMIZE(SIZE) DEBUG OBJECTEXTEND DEFINE(ENABLE_EPROM,LA -NG=UKR,DEBUG,_RESTAURANT_MODE,xTECH_ROM,xPRINT_LOGO_FROM_ROM) CODE LISTINCLUDE SYMBOLS PRINT(.\obj\data.lst) PREPRINT(. -obj\data.i)
//////////////////////////////////////////////////////////////////////// #define BUG_TEST 2 typedef char * PCHAR; typedef char xdata * PCHARx; typedef struct { int I; int J; } A_OLD; typedef struct { int nI; int nJ; } A, *PA; typedef A xdata * PAx; xdata A_OLD cAOld; #define _STRUCT_MEMBER_OFFSET( s, m ) (((PCHAR)&(s).m) - (PCHAR)&(s)) #define _Ax (*(PAx)(&cAOld)) #define _A (*(PA)(&cAOld)) #if BUG_TEST == 0 //Normal PAx l1 = (PAx)&_A; //Get address of struct int l2 = _STRUCT_MEMBER_OFFSET( _A, nI ); //Calc offset of member #elif BUG_TEST == 1 //Cann't initialize (compiler error) PAx l1 = &_Ax; //Get address of struct #elif BUG_TEST == 2 //General fault C51 (system error) int l2 = (unsigned int)_STRUCT_MEMBER_OFFSET( _A, nI ); //Calc offset of member #elif BUG_TEST == 3 //Normal int l2 = (unsigned int)(ptrdiff_t)_STRUCT_MEMBER_OFFSET( _A, nI ); //Calc offset of member #endif
The problem will be fixed in the next version (7.07) that will come out in a few weeks. If you need a patch before that, send an email to: support.intl@keil.com.