Tis*_*sys 9 c++ optimization g++
我编写了一个非常简单的代码来填充具有常量值(1024)的32x32 16bpp图像.图像缓冲区由a托管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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Linux x86_64和gcc 4.6.1(Ubuntu 11.10).该代码运行正常使用-O0,-O1并且-O2优化级别.Valgrind不会报告任何访问冲突.但是,只要切换到-O3或使用-ftree-vectorize自动矢量化选项,程序就会崩溃:
# g++ -g -O3 ./test.cpp -Wall -pedantic && ./a.out
Segmentation fault
# g++ -g -O2 -ftree-vectorize ./test.cpp -Wall -pedantic && ./a.out
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
当我使用-m32gcc标志切换到32位二进制文件时,不会发生崩溃.如果我使用均匀音高(例如pitch = width * 2 + 2),它也不会发生.任何人都可以帮我发现我在代码中犯的(当然是愚蠢的)错误吗?提前谢谢了!
更新:正如Jonathan所建议的,我刚刚向GCC开发人员报告了这个问题:http://gcc.gnu.org/bugzilla/show_bug.cgi? id = 56392
Richard Blener 在gcc Bugzilla上回答了我的问题:
“您正在取消引用指向 uint16_t 的指针,该指针与该类型没有充分对齐。C 标准禁止这样做,从而导致未定义的行为。”
然而,在我看来,应该针对这种未定义的行为生成警告。另请注意,@jmetcalfe 在本文的评论中也给出了这一解释。