分段错误:11;基于范围的 for 循环
Segmentation fault: 11; range-based for loop
我正在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 位。此外,由于我们知道所有值都是正数,因此将其设置为无符号更有意义。
- 基于范围循环到旧编译器的旧样式
- 带有自定义对象的C 范围循环
- 为什么编译器不优化集合元素上的空范围循环?
- 迭代在包含向量的vor in for for范围循环中迭代
- 我们可以在范围循环中使用引用(而不是指针)上的删除
- 如何实现我的自定义范围循环
- C++范围循环
- GCC 4.4 不实现 C++11 范围循环.它还支持哪些其他范围循环语法
- 嵌套c++11范围循环,用于查找组合
- 范围循环中的访问索引
- 对于不可复制类型的范围循环,是否可能
- 我的范围循环出现逻辑错误
- 如何在我的类上允许范围循环
- c++的范围循环比递减循环快
- 通过范围循环从指针容器中获取取消引用元素的引用
- c++ 11:为什么这个范围循环会使FPS下降35
- 带有自定义类的范围循环(矢量的简单版本<string>,使用分配器)
- MSVC10 Visual Studio 2010支持c++范围循环吗?
- 围绕 2 的幂范围循环变量
- toupper() 在 for 范围循环中不起作用