GCC missing braces around initializer

2019-01-31 19:34发布

I have this struct in C below that I want to initialize to all zero. How do I get rid of the missing braces warning?

typedef struct {
    uint32_t incoming[FRAME_TYPE_MAX];
    uint32_t outgoing[FRAME_TYPE_MAX];
    uint32_t timeouts;
    uint32_t crc_errors;
} pkt_t;

static pkt_t stats = {0};

8条回答
甜甜的少女心
2楼-- · 2019-01-31 20:03

From "info gcc"

As a GNU extension, GCC allows initialization of objects with static storage duration by compound literals (which is not possible in ISO C99, because the initializer is not a constant). It is handled as if the object was initialized only with the bracket enclosed list if the types of the compound literal and the object match. The initializer list of the compound literal must be constant. If the object being initialized has array type of unknown size, the size is determined by compound literal size.

 static struct foo x = (struct foo) {1, 'a', 'b'};
 static int y[] = (int []) {1, 2, 3};
 static int z[] = (int [3]) {1};

The above lines are equivalent to the following:

 static struct foo x = {1, 'a', 'b'};
 static int y[] = {1, 2, 3};
 static int z[] = {1, 0, 0};

You may be able to combine these initializers to allow gcc-specific initialization of your arrays without having to specify every element in the array. Or...you can set a flag and initialize it at runtime when necessary, or...you can discover whether the variable is in BSS or not and may be automatically zeroed (is this on the stack in a function or in global memory).

查看更多
闹够了就滚
3楼-- · 2019-01-31 20:05

Set this gcc compiler flag: -Wno-missing-braces

查看更多
Summer. ? 凉城
4楼-- · 2019-01-31 20:07

This is GCC bug # 53119:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119

If you want to see it fixed, post a followup to the bug report indicating that it's a problem for you.

查看更多
混吃等死
5楼-- · 2019-01-31 20:07

If it is a global variable or a local static one, it's automatically initialized. So, simply:

static pkt_t stats;
查看更多
贼婆χ
6楼-- · 2019-01-31 20:11

One way is to initialize every member of the struct inside the braces, rather than relying on the implicit zero filling. For array members, you need another {} which is likely causing the warning. Another is to just disable the warning, though this isn't recommended as it can also catch legitimate bugs.

查看更多
看我几分像从前
7楼-- · 2019-01-31 20:11
#define FRAME_TYPE_MAX 3

typedef struct {
    uint32_t incoming[FRAME_TYPE_MAX];
    uint32_t outgoing[FRAME_TYPE_MAX];
    uint32_t timeouts;
    uint32_t crc_errors;
} pkt_t;

static pkt_t stats1= { .incoming={5,6,20},
                       .outgoing={0,0,0},
                       .timeouts=0,
                       .crc_errors=0
                       };

static pkt_t stats2= { {5,6,20},
                       {0,0,0},
                       0,
                       0
                       };

static pkt_t stats3= {{0}};

pkt_t stats4 ;   // global


int main(void)
{

    stats1.incoming[0]= 35;
    stats1.timeouts=25;
    stats2.incoming[2]=10;  
    stats3.outgoing[2]=10;  
    stats4.timeouts=10;
    for (;;);
    }
查看更多
登录 后发表回答