c - Warning for struct padding in Clang -
i created following structure:
typedef struct s_fct_printf { char flag; void (*fct)(void*); } t_fct_printf; static const t_fct_printf flags[] = { { 's', my_putstr_printf }, //[...] { 'b', display_base_2 }, };
but when compile clang -weverything, have following warning:
warning: padding struct 'struct s_fct_printf' 7 bytes align 'fct' [-wpadded]
i found following solution :
typedef struct s_fct_printf { char flag; void (*fct)(void*); char pad[7]; } t_fct_printf;
but it's not solving problem:
warning: missing field 'pad' initializer [-wmissing-field-initializers] { 'b', display_base_2 }, warning: padding struct 'struct s_fct_printf' 7 bytes align 'fct' [-wpadded]
so tried that:
typedef struct s_fct_printf { char flag; char pad[7]; void (*fct)(void*); } t_fct_printf;
but got following errors:
warning: incompatible pointer integer conversion initializing 'char' expression of type 'void (void *)' [-wint-conversion] { 'b', display_base_2 }, warning: suggest braces around initialization of subobject [-wmissing-braces] { 'b', display_base_2 }, warning: missing field 'fct' initializer [-wmissing-field-initializers] { 'b', display_base_2 }, error: initializer element not compile-time constant { 's', my_putstr_printf },
the last solution found that, have read it's not optimized since compilator not packaging variables anymore.
typedef struct __atribute__((__packed__)) s_fct_printf { char flag; void (*fct)(void*); } t_fct_printf;
is there solution?
typedef struct s_fct_printf { char flag; char pad[7]; void (*fct)(void*); } t_fct_printf;
is taking care of padding issue. however, you'll have change way initialize array.
use
static const t_fct_printf flags[] = { { 's', "", my_putstr_printf }, { 'b', "", display_base_2 }, };
otherwise, compiler tries initialize member pad
my_putstr_printf
, not want.
update
you can avoid hard coded number 7
size of pad
using:
typedef struct s_fct_printf { char flag; char pad[sizeof(void(*)(void))-1]; void (*fct)(void*); } t_fct_printf;
thanks due @weathervane suggestion.
Comments
Post a Comment