具有未定义行为的 C++ 代码,编译器生成 std​​::exception

标签 c++ arrays static

我在 C++ 中遇到了一个有趣的安全编码规则,它指出:

Do not reenter a function during the initialization of a static variable declaration. If a function is reentered during the constant initialization of a static object inside that function, the behavior of the program is undefined. Infinite recursion is not required to trigger undefined behavior, the function need only recur once as part of the initialization.

不合规的例子是:

#include <stdexcept>

int fact(int i) noexcept(false) {
  if (i < 0) {
    // Negative factorials are undefined.
    throw std::domain_error("i must be >= 0");
  }

  static const int cache[] = {
    fact(0), fact(1), fact(2), fact(3), fact(4), fact(5),
    fact(6), fact(7), fact(8), fact(9), fact(10), fact(11),
    fact(12), fact(13), fact(14), fact(15), fact(16)
  };

  if (i < (sizeof(cache) / sizeof(int))) {
    return cache[i];
  }

  return i > 0 ? i * fact(i - 1) : 1;
}

根据来源给出错误:

terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'
  what():  std::exception

Visual Studio 2013 中执行时.我尝试了自己的类似代码并得到了相同的错误(使用 g++ 编译并在 Ubuntu 上执行)。

我怀疑我对这个概念的理解是否正确,因为我不精通 C++。据我说,由于缓存数组是常量,这意味着它可以是只读的并且只需要作为静态初始化一次,它会一次又一次地初始化,因为该数组的值是每个返回的值逗号分隔的递归函数调用,这违反了声明的数组的行为。因此,它给出了未定义的行为,这也在规则中进行了说明。

对此有什么更好的解释?

最佳答案

为了执行fact(),首先需要静态初始化fact::cache[]。为了最初fact::cache,你需要执行fact()。那里有一个循环依赖,这会导致你看到的行为。 cache 只会被初始化一次,但它需要自己初始化才能初始化自己。即使打字也让我头晕目眩。

像这样引入缓存表的正确方法是将其分离成不同的函数:

int fact(int i) noexcept(false) {
  if (i < 0) {
    // Negative factorials are undefined.
    throw std::domain_error("i must be >= 0");
  }

  return i > 0 ? i * fact(i - 1) : 1;
} 

int memo_fact(int i) noexcept(false) {
  static const int cache[] = {
    fact(0), fact(1), fact(2), fact(3), fact(4), fact(5),
    fact(6), fact(7), fact(8), fact(9), fact(10), fact(11),
    fact(12), fact(13), fact(14), fact(15), fact(16)
  };

  if (i < (sizeof(cache) / sizeof(int))) {
    return cache[i];
  }
  else {
    return fact(i);
  }    
} 

这里,memo_fact::cache[] 只会被初始化一次——但它的初始化不再依赖于自身。所以我们没有问题。

关于具有未定义行为的 C++ 代码,编译器生成 std​​::exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397778/

相关文章:

c++ - 在 Eclipse 中对 C++ 方法进行排序/按字母顺序排列(不在大纲中,在代码中)

c++ - 理解带有索引修改的嵌套循环

javascript - 按键合并多个数组

swift - 在 Sprite Kit 中实现生命和分数计数的最佳方式(静态、IoC、其他?)

java - 为什么我不能在接口(interface)中声明静态方法?

c++ - 在 C++ 中使用静态类

c++ - 如何定义指向文件流的静态指针?

javascript - 如何使用map或foreach通过奇数、偶数索引将2个数组合并为一个数组

javascript - 在 promise 中执行 forEach 异步请求吗?

c++ - 我们在生成可执行文件时是否同时使用 libstdc++.a 和 libstdc++.so ?