I'm pretty new to C++, and finding difficulty digging into memory management or things alike. Now I am trying to write some rudimentary parser but has the constructor function exits with a segmentation fault and cannot find out why.
Somewhere on the web has informed me that this is caused by I myself somehow corrupted the stack, but I'm to new to realize the origin of the problem.
class Parser {
public:
int *res, len = 0;
string src;
// this len is the number of seps
Parser(const string &s, char t) : src(s) {
int tmp = -1, flag = TRUE, tmps[MAX];
while (flag) {
tmp = s.find(t, tmp + 1);
if (tmp == -1) {
flag = FALSE;
} else {
tmps[len++] = tmp; // len is now really the length
}
}
res = (int *) malloc(len * sizeof(int));
for (int i = 0; i < len; ++i) {
*(res + i) = tmps[i];
}
}
~Parser() {
free(res);
}
};
When run directly, report "Process finished with exit code -1073741819 (0xC0000005)"; and when in debugging mode, raise "SIGSEGV (Segmentation fault)" at "mov %rax,(%rcx)", the instruction.
Thank all your help, now I have fix my code up, and here I offer a complete solution for my little parser, with usages.
#define MAX 99
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2
#include <iostream>
#include <vector>
using namespace std;
class Parser {
public:
vector<int> res;
int len;
string src;
Parser(string s, char t) : src(move(s)) {
// churn a string and a seperator into a vector of ints
int sep, next_sep, tmp;
for (sep = -1, next_sep = 0; next_sep != -1; sep = next_sep) {
next_sep = src.find(t, sep + 1);
if (next_sep == -1) {
res.push_back(stoi(src.substr(sep+1)));
} else if (sep == -1) {
res.push_back(stoi(src.substr(0, next_sep)));
} else {
res.push_back(stoi(src.substr(sep+1, next_sep - sep - 1)));
}
}
len = res.size();
}
};
class TwoTierParser {
public:
vector<Parser> res;
int len;
string src;
TwoTierParser(string s, char x, char y) : src(move(s)) {
// churn a string and two seperators into a vector of Parsers
int sep, next_sep;
for (sep = -1, next_sep = 0; next_sep != -1; sep = next_sep) {
next_sep = src.find(x, sep + 1);
if (next_sep == -1) {
res.emplace_back(Parser(src.substr(sep+1), y));
} else if (sep == -1) {
res.emplace_back(Parser(src.substr(0, next_sep), y));
} else {
res.emplace_back(Parser(src.substr(sep+1, next_sep - sep - 1), y));
}
}
len = res.size();
}
};
int main() {
char comma = ',', semicolon = ';';
string a;
// cin >> a;
a = "1,2,3,4,5;6,7,8,9,10;11,12,13,14,15";
TwoTierParser parse(a, semicolon, comma);
int row = parse.len;
int col = parse.res[0].len;
int matrix[row][col];
for(int i=0; i<row; ++i){
for(int j=0; j<col; ++j){
matrix[i][j]=parse.res[i].res[j];
}
}
for (int k = 0; k < row; ++k) {
for (int i = 0; i < col; ++i) {
cout<<matrix[k][i]<<" ";
}
cout<<"\n";
}
return 0;
}
User contributions licensed under CC BY-SA 3.0