Update on Question: Here is the SSCCE where I receive SIGSEV
on str1_h[0][j] = bar1(str1_c[j]);
. All variables are not-null which I checked using 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++:
int x[3][4][n];
either use new
, or use an STL container (eg std::vector
). See also How do I declare a 2d array in C++ using new?
Your array x
is on the stack , thus writing to this array will likely overwrite important values on the stack (eg return pointers etc.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.