g ++ -O2 OK,-O3分段错误 [英] g++ -O2 OK, -O3 Segmentation fault

查看:79
本文介绍了g ++ -O2 OK,-O3分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个奇怪的问题。


我用g ++(GCC)4.1.2编译我的代码。如果我使用

-O2优化标志,我的代码运行得很好。 valgrind没有报告任何内存泄漏。


当我使用-O3标志时,一切都崩溃了。程序报告

分段错误。 valgrind报告一些无效的阅读。


此外,如果我在线路附近的某些东西,其中valgrind报告

无效读取,那么一切正常,不再分段错误

或无效读取。


提前致谢,

Rares

推荐答案

Rares Vernica写道:
Rares Vernica wrote:

你好,


我有一个奇怪的问题。


我用g ++(GCC)4.1.2编译我的代码。如果我使用

-O2优化标志,我的代码运行得很好。 valgrind没有报告任何内存泄漏。


当我使用-O3标志时,一切都崩溃了。程序报告

分段错误。 valgrind报告一些无效的阅读。


此外,如果我在线路附近的某些东西,其中valgrind报告

无效读取,那么一切正常,不再分段错误

或无效读取。
Hello,

I have a strange problem.

I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
the -O2 optimization flag. valgrind does not report any memory leak.

When I use the -O3 flag, everything falls apart. The program reports
Segmentation fault. valgrind reports some Invalid read.

Moreover, if I cout something near the line for which valgrind reports
the invalid read, then everything works fine, no more segmentation faults
or invalid reads.



看看最新版本的GCC是否仍然存在。


如果不查看代码,我会指出手指在编译器但是

可能会发生奇怪的事情,可能会暴露你的代码中的错误。


如果是编译器错误,那么提交一个bug来反对GCC - 他们非常好

指向你的解决方法,并在以后的版本中修复bug。


G

See if this persists on the latest version of GCC.

Without looking at the code I''d point the finger at the compiler but
strange things can happen that may expose a bug in your code.

If it is a compiler bug, then file a bug against GCC - they''re very good
at pointing you at a workaround and also fixing the bug in later releases.

G


Gianni Mariani schrieb:
Gianni Mariani schrieb:

不看代码我会指责编译器但是

可能会发生奇怪的事情,可能会暴露代码中的错误。
Without looking at the code I''d point the finger at the compiler but
strange things can happen that may expose a bug in your code.



Eh?不看代码,我会指着程序员。

听起来像是一个明确的调用UB的情况,它改变了它的优化等级的b $ b表现形式。也许堆栈腐败

等。 Valgrind可能会因为-O2错过它,因为没有非法访问带有-O2的
- 故事可能与-O3不同。


有趣的可能是汇编级别调试,对-O3区分-O2

的objdump并检查位置。


问候,

Johannes


-

" Wer etwas kritisiert muss es noch lange nicht selber besser k?nnen。 Es

reicht zu wissen,da? andere es besser k?nnen und andere es auch

besser machen um einen Vergleich zu bringen。 - Wolfgang Gerber

in de.sci.electronics< 47 *********************** @ news.freenet.de>

Eh? Without looking at the code I''d point my finger at the programmer.
Sounds like a definite case of invoked UB which changes its
manifestation with increased optimization level. Maybe stack corruption
or such. Valgrind may miss it with -O2 because no illegal accesses take
place with -O2 - the story may be different with -O3.

Interesting might be assembly level debugging, diff the objdump of -O2
against -O3 and inspect the location.

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser k?nnen. Es
reicht zu wissen, da? andere es besser k?nnen und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>


Rares Vernica写道:
Rares Vernica writes:

你好,


我有一个奇怪的问题。


我用g ++(GCC)4.1.2编译我的代码。如果我使用

-O2优化标志,我的代码运行得很好。 valgrind没有报告任何内存泄漏。


当我使用-O3标志时,一切都崩溃了。程序报告

分段错误。 valgrind报告一些无效的读取。
Hello,

I have a strange problem.

I compile my code with g++ (GCC) 4.1.2. My code runs just fine if I use
the -O2 optimization flag. valgrind does not report any memory leak.

When I use the -O3 flag, everything falls apart. The program reports
Segmentation fault. valgrind reports some Invalid read.



这听起来像你的代码违反了C ++或C的一些细微差别,当它来到指针别名的
时,以及更高的优化级别,gcc'稍微

更高的积极优化最终会破坏你的代码。


编译器错误始终是可能的,但你需要证明它' 'sa

编译器代码。你可以通过发布最少量的完整

代码来证明你的问题。

This sounds like your code violates some nuance of C++ or C, when it comes
to pointer aliasing, and at the higher optimization level, gcc''s slightly
higher aggressive optimization ends up breaking your code.

A compiler bug is always possible, but you need to prove that it''s a
compiler code. And you do that by posting the minimum amount of complete
code that demonstration your problem.


此外,如果我在线附近做某事对于哪个valgrind报告

无效读取,然后一切正常,没有更多的分段错误

或无效读取。
Moreover, if I cout something near the line for which valgrind reports
the invalid read, then everything works fine, no more segmentation faults
or invalid reads.



是的,听起来像-O3'更高的优化级别最终会在代码的逻辑中暴露出一些错误

技术上违反了C ++ / C.


----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.9(GNU / Linux)


iEYEABECAAYFAkhyl3YACgkQx9p3GYHlUOK + HACfSnF7vbhh6Y xdl71Gm114fLk1

FE8AnituISGbVJWuZ8zDNjgSxfcoNO2L

= e3eh

----- END PGP SIGNATURE -----

Yes, sounds like -O3''s higher optimization level ends up exposing some error
in the code''s logic that technically violates C++/C.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhyl3YACgkQx9p3GYHlUOK+HACfSnF7vbhh6Y xdl71Gm114fLk1
FE8AnituISGbVJWuZ8zDNjgSxfcoNO2L
=e3eh
-----END PGP SIGNATURE-----


这篇关于g ++ -O2 OK,-O3分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆