我编写了一个非常简单的代码来用一个常数值(1024)填充一个 32x32 16bpp 图像。图像缓冲区由std::vector
. 我的图像的间距/步幅(即两个连续行之间的字节数)大到足以容纳一整行,但设置为奇数。这是我的代码:
#include <vector>
#include <stdint.h>
int main()
{
int width = 32;
int height = 32;
int pitch = width * 2 + 1;
std::vector<uint8_t> image(height * pitch);
uint8_t* buffer = &image[0];
for (int y = 0; y < height; y++)
{
uint16_t* p = reinterpret_cast<uint16_t*>(buffer + y * pitch);
for (int x = 0; x < width; x++, p++)
{
*p = 1024;
}
}
}
我正在使用带有 gcc 4.6.1(Ubuntu 11.10)的 Linux x86_64。代码在-O0
,-O1
和-O2
优化级别下运行良好。Valgrind 不报告任何访问冲突。但是,一旦我切换到-O3
或使用-ftree-vectorize
auto-vectorization 选项,程序就会崩溃:
# g++ -g -O3 ./test.cpp -Wall -pedantic && ./a.out
Segmentation fault
# g++ -g -O2 -ftree-vectorize ./test.cpp -Wall -pedantic && ./a.out
Segmentation fault
gdb 和 valgrind 都没有提供任何有用的信息:
# valgrind ./a.out
==3840== Memcheck, a memory error detector
==3840== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==3840== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==3840== Command: ./a.out
==3840==
==3840==
==3840== Process terminating with default action of signal 11 (SIGSEGV)
==3840== General Protection Fault
==3840== at 0x4005B3: main (test.cpp:18)
==3840==
==3840== HEAP SUMMARY:
==3840== in use at exit: 2,080 bytes in 1 blocks
==3840== total heap usage: 1 allocs, 0 frees, 2,080 bytes allocated
==3840==
==3840== LEAK SUMMARY:
==3840== definitely lost: 2,080 bytes in 1 blocks
==3840== indirectly lost: 0 bytes in 0 blocks
==3840== possibly lost: 0 bytes in 0 blocks
==3840== still reachable: 0 bytes in 0 blocks
==3840== suppressed: 0 bytes in 0 blocks
==3840== Rerun with --leak-check=full to see details of leaked memory
==3840==
==3840== For counts of detected and suppressed errors, rerun with: -v
==3840== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Segmentation fault
-m32
当我使用gcc 标志切换到 32 位二进制文件时,不会发生崩溃。如果我使用均匀的音高(例如pitch = width * 2 + 2
),它也不会发生。谁能帮我发现我在代码中犯的(当然是愚蠢的)错误?提前谢谢了!
更新:正如乔纳森所建议的,我刚刚向 GCC 开发人员报告了这个问题: http ://gcc.gnu.org/bugzilla/show_bug.cgi?id=56392