Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing complex structure, GCC warns: initialized field with side-effects overwritten

Tags:

c

gcc

c99

I'm coding a backgammon position evaluation system and I'm trying to use designated initalizers, compound literals and a variadic macro to create a keyword argument function like described here , and in Ben Klemens' excellent book 21st Century C.

// from board.h
typedef struct _board_t board_t;  

// from evaluator.h (evaluator is an "interface" in a primitive object system.)
#define EVALUATOR(v) (evaluator_t*)(v)

enum { OUTPUT_WIN, OUTPUT_WINGAMMON, OUTPUT_WINBACKGAMMON,
       OUTPUT_LOSEGAMMON, OUTPUT_LOSEBACKGAMMON, N_OUTPUT };

typedef struct _evaluator_t evaluator_t;

// from composite_evaluator.h
#define N_MAX_EVALUATORS_IN_COMPOSITE 10

typedef evaluator_t * (*new_func)( void *d );

typedef struct {
    int n_eval;
    int (*classifier)( const board_t *b );
    struct {
        new_func construct;
        void *args;
    } config[N_MAX_EVALUATORS_IN_COMPOSITE];
} composite_evaluator_config_t;

// some evaluator implementations (just decl for _new)
typedef struct _overevaluator_t       overevaluator_t;
typedef struct _onesidebearoff_t      onesidebearoff_t;
typedef struct _neuralnet_evaluator_t neuralnet_evaluator_t;
overevaluator_t        *overevaluator_new       (void *args);
onesidebearoff_t       *onesidebearoff_new      (void *args);
neuralnet_evaluator_t  *neuralnet_evaluator_new (void *args);

// and a classifing function decl.
int classify_position( const board_t *b );

// Example from main program
#include <stdio.h>
int main()
{
    // This is of course coded in a variadic macro, but the macro is left out for simplicity of the example.
    composite_evaluator_config_t myeval = {
        .n_eval = 5,
        .classifier = classify_position,
        .config = {
          [0] = { .construct = (new_func)overevaluator_new },
          [1] = { .construct = (new_func)onesidebearoff_new },
          [2] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"race.weights", "race", "race"}) },
          [3] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"contact.weights", "contact", "contact"}) },
          [4] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"crashed.weights", "crashed", "contact"}) },
          // The following line is added by a __VA_ARGS__, hence overriding the [3] above.
          [3] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"td1228.weights", "contact", "contact"}) },
        }
    };

    char **args = (char**) myeval.config[3].args; 
    printf("Neural network weights read from '%s'.\n", args[0]);

    return 0;
}

I believe this is legal C99 (?), however when I compile with GCC (4.9.2 20141224) I get the following warning:

$ gcc -Wall -Wextra -Wno-override-init -c evaltest.c
evaltest.c: In function ‘main’:
evaltest.c:63:13: warning: initialized field with side-effects overwritten
         [3] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"td1228.weights", "contact", "contact"}) },
         ^
evaltest.c:63:13: warning: (near initialization for ‘myeval.config[3]’)

However, when I compile with clang, like this:

clang -Wall -Wextra -Wno-initializer-overrides -c evaltest.c

I get absolutely no warnings at all! However both compilers seems to build the intended code.

So, clang and I think this is OK code. Can someone please explain why GCC send me this warning? Is the warning valid? Can I suppress it in any way? Or is it a GCC bug?

like image 242
Øystein Schønning-Johansen Avatar asked Apr 08 '26 01:04

Øystein Schønning-Johansen


1 Answers

because here

[3] = { .construct = (new_func)neuralnet_evaluator_new, .args = &((char*[]){"td1228.weights", "contact", "contact"}) }

you initalized .config[3] twice

Gcc 4.9 Doc: Designated-Init

If the same field is initialized multiple times, it has the value from the last initialization. If any such overridden initialization has side-effect, it is unspecified whether the side-effect happens or not. Currently, GCC discards them and issues a warning.

like image 169
simon_xia Avatar answered Apr 09 '26 20:04

simon_xia