Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure structures are completly initialized (by name) in GCC?

Tags:

c

gcc

How do I ensure each and every field of my structures are initialized in GCC when using designated initializers? (I'm especially interested in function pointers.) (I'm using C not C++.)

Here is an example:

typedef struct {
    int a;
    int b;
} foo_t;

typedef struct {
    void (*Start)(void);
    void (*Stop)(void);
} bar_t;

foo_t fooo = { 
        5 
    };

foo_t food = { 
        .b=4 
    };

bar_t baro = {
        NULL
    };

bar_t bard = { 
        .Start = NULL
    };

-Wmissing-field-initializers does not help at all. It works for fooo only in GCC (mingw 4.7.3, 4.8.1), and clang does only marginally better (no warnings for food and bard).

I'm sure there is a reason for not producing warnings for designated initializer (even when I explicitly ask for them) but I want/need them. I do not want to initialize structures based on order/position because that is more error prone (for example swapping Start and Stop won't even give any warning). And neither gcc nor clang will give any warning that I failed to explicitly initialize a field (when initializing by name). I also don't want to litter my code with if(x.y==NULL) lines for multiple reasons, one of which is I want compile time warnings and not runtime errors.

At least splint will give me warnings on all 4 cases, but unfortunately I cannot use splint all the time (it chokes on some of the code (fails to parse some C99, GCC extensions)).

Note: If I'm using a real function instead of NULL GCC will also show a warning for baro (but not bard).

I searched google and stack overflow but only found related questions and have not found answer for this specific problem. The best match I have found is 'Ensure that all elements in a structure are initialized' Ensure that all elements in a structure are initialized Which asks pretty much the same question, but has no satisfying answer.

Is there a better way dealing with this that I have not mentioned? (Maybe other code analysis tool? Preferably something (free) that can be integrated into Eclipse or Visual Studio...)

like image 492
Steven Spark Avatar asked Oct 17 '13 15:10

Steven Spark


People also ask

How do you initialize a structure?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).

How do you initialize a struct in C?

Structure members can be initialized using curly braces '{}'.

How do you initialize a struct in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }

Does C initialize structs to 0?

You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.


2 Answers

If I'm not mistaken, the C standards specify that the other fields are automatically initialized with 0.

So what you are asking for - a compilation error when fields are not initialized - would be out of line with the C (modern?) specifications.

C99 standard, page 127 in: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

gccs -Wmissing-field-initializers is documented to not warn with designated initializers. There is a request for an enhancement -Wmissing-field-initializers=2 that would then warn also: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39589

So I suggest you add your wish to that bug report, or maybe even provide a patch. From my experience with open-source software, adding a patch is best.

like image 75
Has QUIT--Anony-Mousse Avatar answered Jan 04 '23 02:01

Has QUIT--Anony-Mousse


The four ways you have showed all initialize the rest of the structure. It's initialized to 0 (or the type equivalent).

like image 40
domen Avatar answered Jan 04 '23 02:01

domen