gpt4 book ai didi

C++14 值初始化问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:35 24 4
gpt4 key购买 nike

谁知道本地 i_local 值在此示例中被零初始化 http://ideone.com/Cqer9Z

#include <iostream>
using namespace std;

int main() {

int i_local; // automatic storage duration, not static

cout << "Value of i_local: " << i_local << endl; // (2-3) value is undetermined
}

它是可变的,具有自动存储期限,根据标准,它应该具有未确定的值。

在我的本地计算机 (c++11) 中它是未确定的,但在 ideone (c++14) 中归零。

最佳答案

完整的标准说(强调):

When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.18). [...] If an indeterminate value is produced by an evaluation, the behavior is undefined except [in some irrelevant cases...]

你有未定义的行为。它可能会打印 0,可能会打印 50,可能会打印出乱码,也可能会删除您的硬盘驱动器。

关于C++14 值初始化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com