Seg fault with -O0 or -O1 flag but not with -O2 or -O3

Seg fault with -O0 or -O1 flag but not with -O2 or -O3

I have a strange issue with an embarrasingly parallel code.When I run it with -O0 or -O1 optimization flags my code throws segfault. However, with -O2 or -O3 flag it runs just fine. The debugger (gdb) says the error is at a OpenMP parallel for directive. I would appreciate if someone has an idea as to what could be the possible reason. I am also debugging in the meantime and will post if I find something. Thank you.

4 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

I figured out the reason for segfault but I am still not sure why the behavior was different for different optimization flags. The reason for segfault was missing a variable in the list of private variables for the OMP parallel for directive.

I've seen this fairly often when writing sequential C++ code. I figure the optimization re-arranges memory accesses in a way that happens to expose the segmentation fault. (A non-allocated region of memory may never be touched, so these bugs go undetected frequently; in this case, my claim is that certain optimizations are simply re-shuffling accesses in a way that exposes the bug.) On a side-note, I always try to run my software through a memory checker like valgrind to make sure I don't have any silent seg-faults waiting to happen (that my code hasn't happened to trigger yet).

Quoting trbot
I've seen this fairly often when writing sequential C++ code. I figure the optimization re-arranges memory accesses in a way that happens to expose the segmentation fault. (A non-allocated region of memory may never be touched, so these bugs go undetected frequently; in this case, my claim is that certain optimizations are simply re-shuffling accesses in a way that exposes the bug.) On a side-note, I always try to run my software through a memory checker like valgrind to make sure I don't have any silent seg-faults waiting to happen (that my code hasn't happened to trigger yet).

That is a very valid point. I second you on the point about Valgrind. An application must pass valgrind (at least --tool=memcheck) with 0 errors, It applies for warnings with -Wall option for gcc flag as well.

发表评论

登录添加评论。还不是成员?立即加入