从值捕获变量分配给Lambda参数时,gcc编译器出现分段错误

乔纳森·米

我正在研究这个答案并编写了代码:

bool generate(vector<int>& temp, int& target, const size_t width, const size_t i) {
    const auto replacement = temp[i];
    const auto result = target > replacement;

    if (result) {
        for_each(begin(temp), next(begin(temp), min(temp.size(), i + width - 1)), [=](auto& it) {
            if (target == it) {
                it = replacement;
            } else if (target < it) {
                --it;
            } });
    }
    target = replacement;
    return result;
}

int main() {
    const vector<char> rooms = { 0b1101,    0b110,  0b1101, 0b110,  0b1100, 0b101,  0b110,
                                 0b1110,    0b1001, 0b110,  0b1011, 0b1010, 0b1111, 0b1010,
                                 0b1000,    0b101,  0b11,   0b1110, 0b1011, 0b1110, 0b1010,
                                 0b1011,    0b1101, 0b101,  0b1,    0b101,  0b11,   0b1011 };
    const size_t width = 7U;
    auto result = 0;
    vector<int> temp(rooms.size());

    for (size_t i = 0U; i < rooms.size(); ++i) {
        const auto toWest = (rooms[i] & 0b1000) == 0;
        const auto toNorth = (rooms[i] & 0b100) == 0;
        const auto toEast = (rooms[i] & 0b10) == 0;
        const auto toSouth = (rooms[i] & 0b1) == 0;
        const auto west = toWest && temp[i - 1] != 0 ? temp[i - 1] : numeric_limits<int>::max();
        const auto north = toNorth && temp[i - width] != 0 ? temp[i - width] : numeric_limits<int>::max();
        const auto east = toEast && temp[i + 1] != 0 ? temp[i + 1] : numeric_limits<int>::max();

        temp[i] = min({ temp[i] != 0 ? temp[i] : numeric_limits<int>::max(), result + 1, west, north, east });

        if (temp[i] == result + 1) ++result;

        if (toWest) result -= generate(temp, temp[i - 1], width, i);
        if (toNorth) result -= generate(temp, temp[i - width], width, i);
        if (toEast) result -= generate(temp, temp[i + 1], width, i);
        if (toSouth) temp[i + width] = temp[i];
    }

    for (auto it = cbegin(temp); it != cend(temp);) {
        for (auto i = 0; i < width; ++i, ++it) cout << *it << '\t';
        cout << endl;
    }

    cout << endl << result << endl;
}

在gcc上编译时,此代码会给出错误:

内部编译器错误:分段错误 it = replacement;

我已经在gcc 5.1、5.2和5.3上对其进行了本地尝试,在所有这些代码上都给出了相同的编译器错误。Clang似乎对代码以及Visual Studio都很满意这只是一个编译器错误,不是我犯的错误,对吗?

乔纳森·米

在lambda中按常量捕获值以及由于分段错误而导致的后续内部编译器错误是gcc 5.1-5.3中的错误:https : //gcc.gnu.org/bugzilla/show_bug.cgi?id=70691

如果您需要修复了此错误的云编译器,则可以在http://coliru.stacked-crooked.com上使用gcc,因为它使用的是gcc 6.1 ,可以解决该问题

gcc 5.4也应该更正此问题:https : //gcc.gnu.org/bugzilla/show_bug.cgi?id=70691#c5
它于16年6月3日发布:https//gcc.gnu.org/gcc- 5 /

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将字符串分配给 C 中的结构变量时出现分段错误

将int分配给数组时出现分段错误

将int.MaxValue分配给变量时,编译器的响应不同

将值分配给指针 C++ 时出现分段错误

lambda捕获的“ this”是不正确的。GCC编译器错误?

尝试调用分配给功能的变量时出现错误

内部编译器错误:gcc中的分段错误。发送可变参数模板到结构时

编译器如何将内存地址分配给变量?

Typescript 编译器允许将 JSON 对象分配给类型化的类变量

AVX2:分配给__m256i类成员时出现分段错误

将字符串分配给stack.top()时出现分段错误

分配给数组时出现分段错误?HackerRank 2D 阵列 - DS

TS 编译器错误:类型 'string' 不可分配给类型 'Status'

为什么分配给substr调用的结果不是编译器错误?

C-尝试将函数的返回值分配给变量会导致分段错误

错误的值分配给变量

为什么可以将寄存器数组名称分配给指针变量而不会发生编译器错误?

捕获 json 值并将这些值分配给 python 列表时出现问题

将值分配给我的结构的char成员时,C分段错误

C - 分段错误将值分配给结构数组

将Razor变量分配给Javascript变量时出现Javascript语法错误

我在 GCC 编译器中遇到分段错误

我在 GCC 编译器中遇到分段错误

这个lambda捕获问题是gcc编译器错误吗?

将类类型分配给泛型变量时,Typescript编译错误

为什么将短变量分配给Integer引用会产生编译时错误?

将值分配给函数参数后,IE中出现预期的')'JS错误

将函数的返回向量分配给另一个向量时出现分段错误

将变量分配给数组时,为什么出现数组下标超出范围错误?