分段错误:11;基于范围的 for 循环

Segmentation fault: 11; range-based for loop

本文关键字:范围 循环 for 于范围 错误 分段      更新时间:2023-10-16

我正在C++做一个小的编程练习。目标是启动一个具有 2 的前 32 个幂的数组,然后输出它们。使用普通的 for 循环没有问题,但我尝试使用 C++11 标准中引入的基于范围的 for 循环。在编译过程中,我收到警告"基于范围的 for 循环是 C++11 扩展 [-Wc++11-extensions]"。运行程序,我收到错误"分段错误:11",没有任何进一步的输出。

我已经知道 elem 变量不知何故被破坏了,但我不知道怎么做。希望你能帮助一个n00b:)

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    const int LAENGE = 32;
    long potenzen[LAENGE];
    for(int elem : potenzen)
    {
        potenzen[elem] = pow(2.0, (double) (elem + 1));
    }
    for(int elem : potenzen)
    {
        cout << endl;
        cout << potenzen[elem];
    }
    cout << endl;
    return 0;
}

>elem被分配在potenzen中的,而不是索引。 cout << elem;是您想要打印数组元素的内容。为了填充数组,只需使用整数索引:

for (int i = 0; i < LENGTH; i++) { // ProTip #1: use English identifiers
    array[i] = 2 << i; // ProTip #2: don't use `pow()` when working with integers
}

至于编译器警告:编译时使用 -std=c++11-std=c++0x 标志,告诉编译器您打算使用 C++11 功能(假设您使用 GCC 或 clang - 我不确定其他编译器。

Ranged for 循环将为您提供元素值,而不是元素索引。

    potenzen[elem] = pow(2.0, (double) (elem + 1));

应该是

for(int i = 0; i < LAENGE; i++)
  potenzen[i] = 2 << i;

(有关转移,请参阅H2CO3的答案以及下面的评论)

请注意,您不能在此处使用 foreach 循环:

for(int& elem : potenzen)
{
    elem = pow(2.0, (double) (elem + 1));
}

当您在语句右侧访问未初始化的 elem 值时。

也:

for(int elem : potenzen)
{
    cout << endl;
    cout << potenzen[elem];
}

应该是

for(int elem : potenzen)
{
    cout << endl;
    cout << elem;
}

因为elem将包含数组值。

上面的答案正确地指出了代码中的问题,但是如果你想让数组索引作为元素值,你必须设置它们,否则它们将被初始化为不确定的(垃圾)值; 以下代码也是一个解决方案,与您尝试执行的操作有些相似:

#include <iostream>
#include <algorithm>
int main()
{
  constexpr auto count = 32;
  unsigned long long values[count] = { }; // initialise elements to 0
  auto i = 0;
  // fill them with their respective index values
  std::generate_n(values, count, [&i] { return i++; });
  for(auto &x : values)
  {
    // without casting the literal 2 would be treated as an int
    x = static_cast<unsigned long long>(2) << x;
    std::cout << x << std::endl;
  }
  return 0;
}

我用unsigned long long代替long,因为在许多系统上,长整型的大小是 4 个字节,但 2^32 = 4294967296 = 0x100000000 即需要 33 位。此外,由于我们知道所有值都是正数,因此将其设置为无符号更有意义。