Segfault from hflip

Segfault from hflip

本文关键字:hflip from Segfault      更新时间:2023-10-16

由于某种原因,当我运行此代码时,我会得到一个segfault。它的作用是从输入中读取PGM文件并水平翻转。

这是我认为是有问题的代码:

for (i = pixels.size()-1; i = 0; i--){ // this loop takes the final value of the original vector and puts it into the first spot in the new hflip vector, and continues onwards
    flippy.push_back(pixels[i]);
}
cout << "P2" << endl << numColumns << " " << numRows << endl << "255" << endl;
while (p < pixTotal){
    for (int z = 0; z < numRows; z++){
        cout << flippy[p] << " ";
    }
    cout << endl;
    p++;
}

你有

for(i = pixels.size() - 1; i = 0; i-)

中间应为

i> = 0

不是

i = 0

我假设向量pixels代表矩阵中的每一行。然后,要翻转向量中的所有值,您只需使用std::reverse_copy即可:

std::vector<uint8_t> flippy;
flippy.resize(pixels.size());
std::reverse_copy(pixels.begin(), pixels.end(), flippy.begin());

您需要为每一行执行此操作。然后,您可以在每个反向后输出每个翻转行,以使矢量" flippy"仅表示当前行。