2016-06-17 26 views
1

Ich wurde am this answer arbeiten und schrieb den Code:gcc-Compiler Segmentierungsfehler bei der Zuordnung von erfassten Wert Variable zu Lambda Parameter

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; 
} 

When compiled on gcc dieser Code gibt den Fehler:

Internal compiler error: Segmentation fault it = replacement;

Ich habe versuchte es lokal auf gcc 5.1, 5.2 und 5.3, es gibt den gleichen Compiler-Fehler auf allen von ihnen. Clang seems happy with the code sowie Visual Studio. Dies ist nur ein Compiler-Bug und kein Fehler, den ich gemacht habe, oder?

+6

Füllen Sie einen Fehlerbericht aus. Ein segfault _im Compiler_ ist niemals der Fehler des Programmierers. – tkausl

Antwort

1

Die Erfassung von Wert einer Konstante Variable in einem Lambda und der anschließenden internen Compiler-Fehler aufgrund eines Segmentierungsfehler ist ein Fehler in gcc 5,1-5,3: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70691

Wenn Sie einen Cloud-Compiler müssen, dass dies Fehler behoben Sie gcc auf http://coliru.stacked-crooked.com verwenden können, weil sie mit gcc 6.1 ist which has resolved the issue

gcc 5.4 sollte auch diese haben korrigiert: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70691#c5
es wurde am Jun freigegeben 3 `16: https://gcc.gnu.org/gcc-5/