我有这个代码:
size_t count = new_data.size();
std::cout << "got the count: " << count << std::endl;
double g_x[count];
std::cout << "First array done\n";
double g_y[count];
std::cout << "Allocated array of size" << count << std::endl;
这给了我输出:
got the count: 1506538
Segmentation fault: 11
老实说,我不明白为什么。它适用于另一个数据集,但不适用于这个数据集。
最佳答案
您可能刚刚遇到堆栈溢出。尝试动态分配内存,即使用堆。
double* g_x = new double[count];
...
delete[] g_x;
更好的解决方案是使用 std::vector<>
:
#include <vector>
...
std::vector<double> g_x(count); // Creates vector with the specified size.
关于C++ 分配段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15043971/