結構填充我創建了以下結構:警告在鏘
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 },
};
但是,當我鏗鏘-Weverything編譯,我有以下警告:
warning: padding struct 'struct s_fct_printf' with 7 bytes to
align 'fct' [-Wpadded]
我發現了以下解決方案:
typedef struct s_fct_printf
{
char flag;
void (*fct)(void*);
char pad[7];
} t_fct_printf;
但它不解決問題:
warning: missing field 'pad' initializer [-Wmissing-field-initializers]
{ 'b', display_base_2 },
warning: padding struct 'struct s_fct_printf' with 7 bytes to
align 'fct' [-Wpadded]
所以,我想的是:
typedef struct s_fct_printf
{
char flag;
char pad[7];
void (*fct)(void*);
} t_fct_printf;
但得到了以下錯誤:
warning: incompatible pointer to integer conversion initializing 'char'
with an 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 is not a compile-time constant
{ 's', my_putstr_printf },
最後的解決方案,我發現了這一點,但我已閱讀,因爲compilator確實它不是最優化不再打包我的變量。
typedef struct __atribute__((__packed__)) s_fct_printf
{
char flag;
void (*fct)(void*);
} t_fct_printf;
有沒有很好的解決方案?
你沒有說明問題... –
嗯,我的問題是,當我用鏗鏘編譯時,我有這樣的警告:padding struct'struct s_fct_printf'with 7 bytes to align'fct'[-Wpadded] – Xwilarg
該警告可以安全地忽略和抑制。填充結構是編譯器經常做的非常有效的事情。除非你依靠特定的佈局,當然,這是不是一件好事.. –