GCC missing braces around initializer

霸气de小男生 提交于 2019-12-18 10:41:05

问题


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};

回答1:


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.




回答2:


Since your first member in the structure is an array you need:

static pkt_t stats = {{0}};

Outer braces are for the struct, inner braces are for the array. However, there are many other ways to skin this cat. (for instance, statics are already init'ed to zero)




回答3:


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

static pkt_t stats;



回答4:


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.




回答5:


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




回答6:


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).




回答7:


#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 (;;);
    }



回答8:


If you still have the joy being on a gcc version which omits this false warning, with a struct like this in the question you can avoid this problem with some simple restructuring like this:

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

static pkt_t stats = {0};


来源:https://stackoverflow.com/questions/11551986/gcc-missing-braces-around-initializer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!