Menu

#3073 block-scope static const uninitialized

closed-fixed
None
other
5
2022-08-30
2020-07-01
No

block-scope static const objects don't get initialized, e.g.

int f(void)
{
  static const int a;
  static const int b;
  return a + b;
}

Might return something other than 0. However, both a and b are static, and should thus get implicitly initalized to 0 at program startup (see 6.7.9p10 in the C2X draft N2479 - AFAIK the same holds in all earlier C standards).

This can easily be reproduced by removing the #if 0 in bug-3040.c regression test.

Related

Bugs: #3040
Wiki: SDCC 4.3.0 Release

Discussion

  • Philipp Klaus Krause

    From the asm code, it looks as if space for these is allocated, as for initalized const variables, but no value is there, i.e. we see just something like

    _f_a_65536_8:
        .ds 1
    

    to be placed somewhere in read-only memory.

     
  • Philipp Klaus Krause

    This also affects other const objects that should be default-initialized, and still happens in SDCC from current trunk:

    const int a;
    static const int b;
    
    int f(void)
    {
      static const int a;
      static const int b;
      return a + b;
    }
    
     

    Last edit: Philipp Klaus Krause 2022-08-30
  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    Fixed in [r13674].

     

    Related

    Commit: [r13674]


Log in to post a comment.

MongoDB Logo MongoDB