Menu

#3564 setting pointer in struct initializer ignores other members

closed-fixed
None
Front-end
5
2023-03-15
2023-03-14
Under4Mhz
No

In the below code, setting the member .current to a value in the initializer, resets the value .x to 0. Removing .current from the initializer, .x gets set to 110 as expected.

/// GPL 2.0 or later

#include <stdint.h>
#include <stdio.h>

typedef struct {

    uint8_t *current;
    int x;

} BlockData;


int main() {

    BlockData block = { .x = 110, .current = 0 };

    printf("Start\n");
    printf("Position x %d current %d\n", block.x, block.current );
    printf("End\n");

    return 0;
}

#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {

    sif = 'p';
    sif = c;

    return c;
}
#endif

// sdcc -mz80 --fverbose-asm ./struct_set.c  -o struct_set.ihx && ucsim_z80 -I if=outputs[0xff] struct_set.ihx
$ sdcc -mz80 --fverbose-asm ./struct_set.c  -o struct_set.ihx && ucsim_z80 -I if=outputs[0xff] struct_set.ihx
./struct_set.c:19: warning 283: function declarator with no prototype
0> Loading from struct_set.ihx
3209 words read from struct_set.ihx
r
Simulation started, PC=0x000000
Start
Position x 0 current 0
End

$ gcc ./struct_set.c && ./a.out
Start
Position x 110 current 0
End
$ sdcc --version
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 TD- 4.2.14 #13904 (Linux)

Related

Wiki: NGI0-Entrust-SDCC

Discussion

  • Philipp Klaus Krause

    I can reproduce this issue in current SDCC 4.2.14 #13904 on Debian GNU/Linux testing on am64.
    Looking at the generated code, each member of block is actually written twice: first with the correct value, then it is overwritten with 0.

    P.S.: Looks like the bug happens in createIvalStruct in src/SDCCast.c

     

    Last edit: Philipp Klaus Krause 2023-03-15
  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
    • Category: other --> Front-end
     
  • Philipp Klaus Krause

    Fixed in [r13905].

     

    Related

    Commit: [r13905]


Log in to post a comment.

MongoDB Logo MongoDB