[英]Segmentation Fault when variables are not-null but on `callq`
Update on Question: Here is the SSCCE where I receive SIGSEV
on str1_h[0][j] = bar1(str1_c[j]);
问题更新:这是我在str1_h[0][j] = bar1(str1_c[j]);
上收到SIGSEV
的str1_h[0][j] = bar1(str1_c[j]);
. . All variables are not-null which I checked using gdb:我使用gdb检查过的所有变量都不是空的:
#include <fstream> //ifstream and ofilestream
using namespace std;
int bar1(int);
int bar2(int);
int bar3(int);
int main() {
string str1 = "string";
ifstream ifs;
ifs.open("./file.txt");
string line, str2;
while (!ifs.eof()) {
ifs >> line;
str2 += line;
}
ifs.close();
unsigned str2_l = str2.length();
unsigned str1_l = str1.length();
unsigned n = str2_l - str1_l + 1;
int str1_c[4];
str1_c[0] = 0;
str1_c[1] = 0;
str1_c[2] = 0;
str1_c[3] = 0;
int x[3][4][n];
int str1_h[3][4];
for (unsigned j = 0; j < 4; j++) {
str1_h[0][j] = bar1(str1_c[j]);
str1_h[1][j] = bar2(str1_c[j]);
str1_h[2][j] = bar3(str1_c[j]);
for (unsigned i = 0; i < n; i++) {
x[0][j][i] = bar1(0);
x[1][j][i] = bar2(0);
x[2][j][i] = bar3(0);
}
}
}
int bar1(int x) {
return 0;
}
int bar2(int x) {
return 0;
}
int bar3(int x) {
return 0;
}
You cannot have a dynamic array on the stack in C++:在 C++ 中,堆栈上不能有动态数组:
int x[3][4][n];
either use new
, or use an STL container (eg std::vector
).要么使用new
,要么使用 STL 容器(例如std::vector
)。 See also How do I declare a 2d array in C++ using new?另请参阅如何使用 new 在 C++ 中声明二维数组?
Your array x
is on the stack , thus writing to this array will likely overwrite important values on the stack (eg return pointers etc.)您的数组x
在堆栈上,因此写入此数组可能会覆盖堆栈上的重要值(例如返回指针等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.