Menu

#989 Shared compound literals

open
nobody
None
5
2025-03-16
2025-03-16
No

C allows sharing memory for compund literals in some cases (section 6.5.3.6): "String literals, and compound literals with const-qualified types, including those specified with constexpr, are not required to designate distinct objects." (quote from the C23 standard, but C99 had similar wording, except for constexpr, which is a C23 feature).

This allows to save memory. SDCC currently implements this for strings literals, but not compound literals:

struct a
{
    int i;
    int j;
};

void f(const char *, struct a);

void g(void)
{
    f("test", (static const struct a){1, 2});
    f("test", (static const struct a){1, 2});
}

When compiling this via sdcc -mstm8 --std-c23 test.c, the resulting assembler code contains the string only once, but the compound literal twice.

Discussion


Log in to post a comment.

MongoDB Logo MongoDB