1

我认为以下程序应该抱怨它不能编译正则表达式,或者将其视为合法并编译它(我没有标准,所以我不能确定表达式是否严格合法;当然合理的解释是可能的)。无论如何,发生的事情g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1是,在运行时,它会严重崩溃

*** Error in `./a.out': free(): invalid next size (fast): 0x08b51248 ***

在图书馆的内脏。

问题是:

a)这是错误,对吗?我假设(也许是错误的)标准没有说 std::regex 如果它不喜欢语法会崩溃。(msvc 吃得很好,fwiw)

b)如果它是一个错误,是否有一些简单的方法可以查看它是否已被报告(我第一次在 gnu-land 错误系统周围探查是令人生畏的)?

#include <iostream>
#include <regex>

int main(void)
    {
    const char* Pattern = "^(%%)|";
    std::regex Machine;

    try {
        Machine = Pattern;
        }
    catch(std::regex_error e)
        {
        std::cerr << "regex could not compile pattern: "
          << Pattern << "\n"
          << e.what() << std::endl;
        throw;
        }

    return 0;
    }
4

1 回答 1

1

我会把它放在评论中,但我不能,所以......

不知道大家有没有知道,不过好像是管道 | 最后导致你的问题的字符。似乎 | 的字符表示 当正则表达式尝试调用 free() 时,g++ 给出的最后一个字符(因为 "^(%%)|a" 对我来说很好用);

该标准(或至少我正在阅读的在线草案)声称:

28.8
Class template basic_regex
[re.regex]

1 For a char-like type charT, specializations of class template basic_regex represent regular expressions
constructed from character sequences of charT characters. In the rest of 28.8, charT denotes a given char-
like type. Storage for a regular expression is allocated and freed as necessary by the member functions of
class basic_regex.

2 Objects of type specialization of basic_regex are responsible for converting the sequence of charT objects
to an internal representation. It is not specified what form this representation takes, nor how it is accessed by
algorithms that operate on regular expressions.
[ Note: Implementations will typically declare some function
templates as friends of basic_regex to achieve this — end note ]

然后,

basic_regex& operator=(const charT* ptr);

3 Requires: ptr shall not be a null pointer.

4 Effects: returns assign(ptr).

所以除非 g++ 认为 const char* Pattern ="|"; 是一个空指针(我想不是......),我猜这是一个错误?

编辑:顺便说一句,连续|| (即使不是在最后)似乎也对我造成了分段错误。

于 2014-01-24T08:07:27.330 回答