简体   繁体   English

使用“-ftrivial-auto-var-init”保证非静态聚合对象的填充字节初始化为零

[英]Using `-ftrivial-auto-var-init` to guarantee the initialization of padding bytes to zero for non-static aggregate objects

I'm following Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects?我正在关注Does C++ standard 保证将非静态聚合对象的填充字节初始化为零? and trying to force GCC and Clang to initialize padding with zero.并试图强制GCC 和 Clang 用零初始化填充。 The -ftrivial-auto-var-init=choice flag seems to do the trick, but I don't see any difference between using it or not -ftrivial-auto-var-init=choice标志似乎可以解决问题,但我看不出是否使用它有什么区别

The original code is here (without using -ftrivial-auto-var-init=zero ), and produces non-zeroed members:原始代码在这里(不使用-ftrivial-auto-var-init=zero ),并生成非零成员:

GCC GCC

Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0

Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0

Clang Clang

Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0

Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0

I've added the -ftrivial-auto-var-init=zero flag to both GCC 12 and Clang 15 .我已将-ftrivial-auto-var-init=zero标志添加到GCC 12 和 Clang 15 but the results are exactly the same, so I'm a bit confused as to what -ftrivial-auto-var-init=zero should do.但结果完全一样,所以我对-ftrivial-auto-var-init=zero应该做什么感到有点困惑。

GCC GCC

Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0

Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0

Clang Clang

Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0

Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0

Note that -ftrivial-auto-var-init only applies to automatic variables (like the name suggests):请注意, -ftrivial-auto-var-init仅适用于自动变量(顾名思义):

Initialize automatic variables with either a pattern or with zeroes to increase the security and predictability of a program by preventing uninitialized memory disclosure and use.使用模式或零初始化自动变量,以通过防止未初始化的 memory 泄露和使用来提高程序的安全性和可预测性。

Automatic variables are variables that have automatic storage duration , ie local block-scope variables only.自动变量是具有自动存储持续时间的变量,即仅本地块范围变量。

Note that if you're using new / delete then your objects will havedynamic storage duration and therefore -ftrivial-auto-var-init will not apply to them.请注意,如果您使用的是new / delete ,那么您的对象将具有动态存储持续时间,因此-ftrivial-auto-var-init将不适用于它们。


Example例子

This example demonstrates the effect of -ftrivial-auto-var-init=zero :此示例演示了-ftrivial-auto-var-init=zero的效果:
godbolt神栓

struct Foo
{
    char x;
    int y;
    char z;
};

int main()
{
    {
        char buf[sizeof(Foo)];
        for(int i = 0; i < sizeof(buf); ++i)
        {
            buf[i] = 'A' + i;
        }
        buf[sizeof(Foo)-1] = '\0';
        printf("%s\n", buf);
    }
    {
        Foo f;
        DumpF("Foo, automatic", &f);
    }
}

When compiled with -ftrivial-auto-var-init=zero you're guaranteed to get:当使用-ftrivial-auto-var-init=zero编译时,你一定会得到:

Foo, automatic:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0

Whereas if you compile without it you'll most likely get random bytes from the stack (in this case gcc chose to reuse the space of buf ):而如果你在没有它的情况下进行编译,你很可能会从堆栈中获取随机字节(在这种情况下 gcc 选择重用buf的空间):

Foo, automatic:
x:[0x41][0x42][0x43][0x44],v: 65
y:[0x45][0x46][0x47][0x48],v: 1212630597
z:[0x49][0x4A][0x4B][----],v: 73

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 标准是否保证非静态聚合对象的填充字节初始化为零? - Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects? 简单的结构:使用非静态成员初始化,聚合初始化或构造函数的优缺点 - Simple structs: advantages/disadvantages of using non-static member initialization, aggregate initialization or constructors 部分聚合初始化和非静态数据成员初始化程序 - Partial Aggregate Initialization and Non-static Data Member Initializer 在Java中的JNI中使用非静态对象 - Using non-static objects in jni in java 具有非静态成员初始值设定项的类的C ++ 11聚合初始化 - C++11 aggregate initialization for classes with non-static member initializers 非静态数据成员初始化 - Non-static data member initialization Visual Studio 2013因非静态数据成员初始化而崩溃 - Visual Studio 2013 crashes for non-static data member initialization 使用新表达式进行非静态数据成员初始化 - non-static data member initialization with new expression 在构造函数初始化列表中调用非静态函数,C ++ - Call non-static function in constructor initialization list, C++ 使用非静态数据成员和嵌套类构造函数的类内初始化时出错 - Error when using in-class initialization of non-static data member and nested class constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM