C251 memory pointer problem

Hi,
Is this a valid construct in you Keil C-251 compiler?

unsigned char data buf[20];

typedef struct {
int i;
int j;
} TEST_T;

#define GetStruct() (*(TEST_T *)&buf[2])

void Test(void) {
GetStruct().i = 3;
}

Notice that buf is in the data area and I'm type casting it to a TEST_T struct in the default memory area (which is in far). V3.53 of the compiler compiles buggy code when similar scheme is used. If I change the buf memory area to "near", or if I change the macro to

#define GetStruct() (*(TEST_T data *)&buf[2])

or

#define GetStruct() (*(TEST_T *)(unsigned char *)&buf[2])


then it compiles good code. Question is, was there something fundamentaly wrong with what I did in the macro?

Andy

Parents
  • Notice that buf is in the data area and I'm type casting it to a TEST_T struct in the default memory area (which is in far).

    No. That is not correct. There is no memory area associated with the type cast. There is only a data type. Besides, if an object is in data memory and you cast a pointer to it into a pointer into a different memory area, then of course the pointer is pointing to the wrong thing.

    Anyway, when I compile you example, the compiler generates the same code no matter what memory model I use. And, the code generated works OK.

    C251 COMPILER V3.53, COMPILATION OF MODULE main
    OBJECT MODULE PLACED IN main.OBJ
    COMPILER INVOKED BY:
    C:\Keil\C251\BIN\C251.EXE main.c LARGE INTR2 BROWSE DEBUG CODE SYMBOLS
    
    stmt  level    source
    
        1          unsigned char data buf[20];
        2
        3          typedef struct {
        4          int i;
        5          int j;
        6          } TEST_T;
        7
        8          #define GetStruct() (*(TEST_T *)&buf[2])
        9
       10          void main(void) {
       11   1      GetStruct().i = 3;
       12   1      }
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
    ;       FUNCTION main (BEGIN)
                                                 ; SOURCE LINE # 10
                                                 ; SOURCE LINE # 11
    000000 7E340003       MOV      WR6,#03H
    000004 7A3500      R  MOV      buf+2,WR6
                                                 ; SOURCE LINE # 12
    000007 22             RET
    ;       FUNCTION main (END)
    

    Can you provide more information about the problem you are having and why you think the compiler generates the wrong code?

    Jon

Reply
  • Notice that buf is in the data area and I'm type casting it to a TEST_T struct in the default memory area (which is in far).

    No. That is not correct. There is no memory area associated with the type cast. There is only a data type. Besides, if an object is in data memory and you cast a pointer to it into a pointer into a different memory area, then of course the pointer is pointing to the wrong thing.

    Anyway, when I compile you example, the compiler generates the same code no matter what memory model I use. And, the code generated works OK.

    C251 COMPILER V3.53, COMPILATION OF MODULE main
    OBJECT MODULE PLACED IN main.OBJ
    COMPILER INVOKED BY:
    C:\Keil\C251\BIN\C251.EXE main.c LARGE INTR2 BROWSE DEBUG CODE SYMBOLS
    
    stmt  level    source
    
        1          unsigned char data buf[20];
        2
        3          typedef struct {
        4          int i;
        5          int j;
        6          } TEST_T;
        7
        8          #define GetStruct() (*(TEST_T *)&buf[2])
        9
       10          void main(void) {
       11   1      GetStruct().i = 3;
       12   1      }
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
    ;       FUNCTION main (BEGIN)
                                                 ; SOURCE LINE # 10
                                                 ; SOURCE LINE # 11
    000000 7E340003       MOV      WR6,#03H
    000004 7A3500      R  MOV      buf+2,WR6
                                                 ; SOURCE LINE # 12
    000007 22             RET
    ;       FUNCTION main (END)
    

    Can you provide more information about the problem you are having and why you think the compiler generates the wrong code?

    Jon

Children
More questions in this forum