【问题标题】:C++11/g++4.8 regex_match segmentation fault on Fedora 19Fedora 19 上的 C++11/g++4.8 regex_match 分段错误
【发布时间】:2013-08-17 07:35:49
【问题描述】:
#include <regex>
#include <iostream>

using namespace std;

void out(bool b)
{
    cout<< ( b ? "found" : "not found" )<<endl;
}

int main()
{

    // find XML/HTML-tagged value(tags before and after the value must match):
    //regex reg2("<(.*)>.*</\\1>");

    regex reg2(R"(<(.*)>.*</\1>)");
    bool found = regex_match("<tag>value</tag>",
            reg2);
    out(found);
}

$ g++ -g -std=c++11 regex1.cpp

$ ./a.out

Segmentation fault (core dumped)


$ gdb a.out core.12473 

GNU gdb (GDB) Fedora (7.6-30.fc19)
Reading symbols from /home/neo/code/regex/a.out...done.
[New LWP 12473]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x0804a352 in std::__detail::_StateSeq::_M_append (this=0xbf948a30, __rhs=...)
    at /usr/include/c++/4.8.1/bits/regex_nfa.tcc:157
157       _M_nfa[_M_end2]._M_next = __rhs._M_start;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-11.fc19.i686 libgcc-4.8.1-1.fc19.i686 libstdc++-4.8.1-1.fc19.i686
(gdb) bt
#0  0x0804a352 in std::__detail::_StateSeq::_M_append (this=0xbf948a30, __rhs=...)
    at /usr/include/c++/4.8.1/bits/regex_nfa.tcc:157
#1  0x0804ea33 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:779
#2  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#3  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#4  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#5  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#6  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#7  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#8  0x0804dc21 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_disjunction (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:758
#9  0x0804ce38 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_Compiler (this=0xbf948c68, 
    __b=@0xbf948d34: 0x80538ff "<(.*)>.*</\\1>", __e=@0xbf948d0c: 0x805390c "", __traits=..., __flags=16)
    at /usr/include/c++/4.8.1/bits/regex_compiler.h:729
#10 0x0804bb51 in std::__detail::__compile<char const*, std::regex_traits<char> > (
    __b=@0xbf948d34: 0x80538ff "<(.*)>.*</\\1>", __e=@0xbf948d0c: 0x805390c "", __t=..., __f=16)
    at /usr/include/c++/4.8.1/bits/regex_compiler.h:1105
#11 0x0804b0bb in std::basic_regex<char, std::regex_traits<char> >::basic_regex (this=0xbf948d4c, 
    __p=0x80538ff "<(.*)>.*</\\1>", __f=16) at /usr/include/c++/4.8.1/bits/regex.h:388
#12 0x08049847 in main () at regex1.cpp:17

【问题讨论】:

  • GCC 不支持&lt;regex&gt;
  • 正如 Rapptz 提到的,gcc 还不支持&lt;regex&gt;here 是它为什么不这样做的详细信息,如果你有兴趣的话。
  • @Praetorian GCC4.8 支持正则表达式。 #include #include 使用命名空间标准; void out(bool b) { cout.**>"); bool found = regex_match("value", reg1);出(找到); } $ g++ -g -std=c++11 regex1.cpp $ ./a.out found $
  • 感谢@falsetru 正则表达式 reg2(R"(.*\\1>)"); // 工作正则表达式 reg2(".*\\1>"); // 不工作你知道为什么吗?
  • 我不知道 C++ 中的原始字符串。

标签: c++ regex c++11 fedora g++4.8


【解决方案1】:

GCC 直到 4.9 才完全支持 C++11 正则表达式(撰写本文时的当前主干)。有关详细信息,请查看“运行时库 (libstdc++)”部分下的 http://gcc.gnu.org/gcc-4.9/changes.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多