在一个循环的语句中出现Segmentation fault: 11的错误
问题出现的平台版本及自己尝试过哪些方法
平台版本: MacOSX 10.14
尝试过的方法: 查看是否有内存未被释放;查看是否有越界访问
相关代码
isOperator 定义处
bool BerryMath::isOperator(char c)
{
return (
c == '+' || c == '-' || c == '*' || c == '/' || c == '%' ||
c == '^' || c == '|' || c == '&'
);
}
出错函数的内容
Segmentation fault: 11出现的位置再注释// Get the first value后面
void BerryMath::expr::parse_expr(std::string s)
{
if (p != 0x0 && --p->use == 0) {
delete p;
}
// Remove blank
int i = 0;
while (i < s.length() && BerryMath::isSpace(i)) {
i++;
}
std::string token = "";
token = "";
// Get the first value
while (i < s.length() && !BerryMath::isOperator(s[i])) {
token += s[i];
i++;
}
std::string v = token;
std::cout << token << std::endl;
// std::cout << token << std::endl;
token = "";
// Get the operator
while (i < s.length() && BerryMath::isSymbol(s[i])) {
token += s[i];
i++;
}
std::string op = token;
// Remove blank
while (i < s.length() && BerryMath::isSpace(s[i])) {
i++;
}
// Get the next operator
while (i < s.length() && BerryMath::isSymbol(s[i])) {
token += s[i];
i++;
}
std::string nextOp = token;
// Get the next expression
token = "";
for ( ; i < s.length() ; i++) {
token += s[i];
}
BerryMath::expr test(op,v);
if (!token.empty()) {
BerryMath::expr a("");
a.parse_expr(token);
*this = BerryMath::expr(
nextOp,
test,
a
);
} else {
*this = BerryMath::expr(
op,
v
);
}
// std::cout << *this << std::endl;
}
我期望语法树创建成功,然而却出现了Segmentation fault: 11的错误
问题解决,是因为另一个函数中的内存分配错误导致的