当使用花括号初始化结构时,gcc会将其填充置为零

问题描述 投票:0回答:1

gcc在c.b之后的3个填充字节是否为零?

struct {
   int a;
   char b;
} c = { 1, 2};
c gcc
1个回答
0
投票

不一定。在x86-64上使用-O2的gcc 10.1,此代码(also on godbolt

struct foo  {
   int a;
   char b;
};

void blah(struct foo *p);

void foo(void) {
    struct foo c = { 1, 2};
    blah(&c);
}

编译到该程序集

foo:
        subq    $24, %rsp
        leaq    8(%rsp), %rdi
        movl    $1, 8(%rsp)
        movb    $2, 12(%rsp)
        call    blah
        addq    $24, %rsp
        ret

您可以看到最后三个字节未初始化。

© www.soinside.com 2019 - 2024. All rights reserved.