【问题标题】:g++ internal compiler error segmentation fault with recursive constexprg ++内部编译器错误分段错误与递归constexpr
【发布时间】:2016-12-22 21:29:15
【问题描述】:

我最近将我的 g++ 版本更新为 6.3.0 (g++ (Homebrew GCC 6.3.0) 6.3.0),但现在我得到了g++: internal compiler error: Segmentation fault: 11 (program cc1plus)。 使用以前的版本(我不完全确定,但在附近)5.2 一切正常。在我的另一台计算机上,我使用 g++ (Ubuntu 5.2.1-22ubuntu2) 5.2.1,这也可以。

代码是:

constexpr bool checkForPrimeNumber(const int p, const int t)
{
    return p <= t or (p % t and checkForPrimeNumber(p, t + 2));
}

constexpr bool checkForPrimeNumber(const int p)
{
    return p == 2 or (p & 1 and checkForPrimeNumber(p, 3));
}

int main() 
{
    static_assert(checkForPrimeNumber(65521), "bug...");
}

我用

编译代码
g++ test.cpp -std=c++11 -fconstexpr-depth=65535

我可以做些什么来解决这个问题?

编辑:

错误报告sumitted

【问题讨论】:

  • “内部编译器错误” - 是编译器中的一个近乎自动的错误,应该这样归档。在您的平台上使用clang(它是Mac,对吗?),静态断言以正确的原因开箱即用:constexpr eval递归深度到512。如果g ++打算这样做,我认为它会比喷出内部编译器错误和段错误更健壮。
  • @WhozCraig clang 和 g++ 的递归限制都是 512。这就是我使用 -fconstexpr-depth=65535 的原因。使用 clang(Apple LLVM 版本 8.0.0 (clang-800.0.42.1))我得到 clang: error: unable to execute command: Segmentation fault: 11
  • Lolz,好吧,你给了我一个新的、可预测的方式来让 Xcode 崩溃;谢谢你=P。这些都不应该崩溃,而且都是供应商恕我直言的错误。
  • 作为一种解决方法:您是否尝试过编写它的迭代版本(需要 c++14)?
  • @downvoter 怎么了?

标签: c++ c++11 g++ c++14


【解决方案1】:

错误来自 g++ 内部的堆栈溢出。据称我能够增加堆栈(在 macOS 10.11.6 上)。但是,它并没有解决手头的问题。 我想出了另一个解决方案,将检查分成两个分支,代码如下:

constexpr bool checkForPrimeNumber(const int p, const int t, const int hi)
{
    return p < hi and (p <= t or (p % t and checkForPrimeNumber(p, t + 2, hi)));
}

constexpr bool checkForPrimeNumber(const int p)
{
    return p == 2 or (p & 1 and (checkForPrimeNumber(p, 3, 32768) or checkForPrimeNumber(p, 3+32768, 65536)));
}

int main()
{
    static_assert(checkForPrimeNumber(65521), "");
}

谢谢

编辑:

根据 cmets 的建议,一个解决方案可能是使用 C++14:

constexpr bool checkForPrimeNumber(const int p)
{
    if (p < 2)
        return false;
    if (p == 2)
        return true;
    if (~p & 1)
        return false;
    for (int i = 3; i < p; i += 2)
    {
        if (p % i == 0)
            return false;
    }
    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2017-08-12
    • 2017-11-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2020-05-21
    • 2016-12-06
    相关资源
    最近更新 更多