提升正则表达式运行时错误
Boost regex runtime error
我正在尝试使用我在另一台计算机上编写的一些代码,将字符串拆分为标记。此代码编译良好。该代码在其他一些计算机上也可以按预期工作。我已经设法将代码减少到以下内容:
#include <string>
#include <boost/regex.hpp>
typedef std::vector<std::string> token_t ;
token_t generate_tokens(std::string raw_input){
//this function breaks a input string into tokens. So test 100 goes to 2 tokens "test" and "100".
boost::regex re_splitter("\s+"); //this uses the regex s+ to find whitespace. The + finds one or more whitespace characters.
boost::sregex_token_iterator iter(raw_input.begin(), raw_input.end(), re_splitter, -1);
//this breaks the string using the regex re_splitter to split into tokens when that is found.
boost::sregex_token_iterator j; //This is actually in the Boost examples, j is the equivalent of end. Yes this did also seem weird to me at first...
token_t token_vector;
unsigned int count = 0;
while(iter != j)
{
token_vector.push_back(*iter);
std::cout << *iter++ << std::endl;
++count;
}
return token_vector;
}
int main(){
std::string in;
int amount = -1;
std::cout << "action: ";
std::getline(std::cin, in);
boost::regex EXPR("^test \d*(\.\d{1,2})?$");
bool format_matches = boost::regex_match(in, EXPR);
token_t tokens = generate_tokens(in);
if(format_matches){
amount = atoi(tokens.at(1).c_str());
}
std::cout << "amount: " << amount << "n";
return 0;
}
这将使用以下方法编译而没有错误或警告: g++ -Wall test.cpp -lboost_regex
但是当在运行时使用时,提供输入test 100
程序会失败。
操作:测试 100
a.out:/usr/local/include/boost/smart_ptr/shared_ptr.hpp:412: typename boost::d etail::shared_ptr_traits::reference boost::shared_ptr::operator*() const [with T = boost::regex_traits_wrapper>>]: 断言 'px != 0' 失败。
中止
我完全不知道这里发生了什么。这是我的代码还是库中的错误?非常感谢任何调试建议!
这不是一个错误。这是提升头文件的冲突。
这可能是因为错误的文件包含,或者因为错误的库包含(正则表达式模块是为数不多的需要编译的 boost 模块之一)。
您应该通过使用 -l 和 -I 开关来确保使用正确的文件,例如:
g++ -W -Wall main.cpp $(LDFLAGS) -lboost_regex -I/data1/PROJECT_SEARCH/libsrc/boost_1_46_1
当您使用提升版本的标头编译并使用另一个提升版本执行时,就会发生这种情况。您应该检查安装了哪个 boost 库来执行,以及用于编译。
在 gdb 或类似程序中运行它,在这些部分的开头设置断点,然后逐步执行,直到找到有问题的行。
您收到的错误看起来像是将无效指针传递到某处的 boost 库。
由于您在代码中没有使用shared_ptr
,并且我看不到其他看起来错误的东西并且它可以在其他机器上运行,因此我会说这可能是 Boost.Regex 中的一个错误。
我敢打赌其他机器安装了其他版本的 boost?
如果我不得不猜测,我会先尝试更改std::cout << *iter++ << std::endl;
行> std::cout << *iter << std::endl; ++iter;
。
像瑞士建议的那样在调试器中运行它,并查看断言的触发位置。
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- C++ 使用增强正则表达式库时断言崩溃
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 使用向量将给定表达式转换为波兰表示法时出现运行时错误
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 当我在每一帧中多次使用正则表达式时,FPS 下降了
- std::正则表达式捕获组语法错误
- 带有正则表达式的 c++ 中的错误
- 在带有brew的MacOS上链接时缺少增强ICU正则表达式符号
- 运行时错误:基0x000000000000溢出到0xffffffffffffffff的指针索引表达式 (basic_st
- 正则表达式在 Windows 上初始化时崩溃
- 为什么这个正则表达式在 STL 世界中是错误的?
- C++抛出错误"Invalid special open parenthesis."中的正则表达式
- 正则表达式分段错误
- 使用boost正则表达式时出现shared_ptr错误
- 运行正则表达式时未选中的异常 - 从文件路径获取不带扩展名的文件名
- 在c++中使用正则表达式时出现错误
- 提升::正则表达式在长表达式不匹配时抛出错误
- 提升正则表达式运行时错误