在 g++ 中启用严格的别名警告

发布于 2024-12-21 22:16:44 字数 48 浏览 4 评论 0 原文

在 g++ 中启用严格别名警告的正确方法是什么? VC++ 10 执行这些规则吗?

What is the correct way to enable strict aliasing warnings in g++? Does VC++ 10 implement those rules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-12-28 22:16:44

g++ 使用 -fstrict-aliasing。我还使用 -Wstrict-aliasing=2 来查看与可能违反严格别名规则相关的警告。

Use -fstrict-aliasing for g++. I also use -Wstrict-aliasing=2 to see warnings related to possible violations of strict aliasing rules.

温柔嚣张 2024-12-28 22:16:44

它们由 -O2 自动启用,因为它需要使用它们来进行一些优化。一定要将其与警告结合起来(-Wall 就可以了),以确保您不会构建潜在的错误代码。否则,您可以使用 -fstrict-aliasing (如另一个答案中所示)来启用它们。

不过我不确定 VC++10 。

They're enabled automatically by -O2 because it needs to use them to do some of the optimizations. Definitely combine it with the warning (-Wall does the trick) to make sure you aren't building potentially buggy code. Otherwise you can use -fstrict-aliasing as seen in another answer to enable them.

I'm not sure about VC++10 however.

瑕疵 2024-12-28 22:16:44

VC++ 10 启用 /O1 及以上版本的严格别名规则。我使用 此处。并获得以下 asm 代码。您可以看到 b 的加载仅完成一次。

00A910AE  movzx       edx,word ptr [edx+2]  //Load of b
00A910B2  xor         eax,eax  
00A910B4  xor         ecx,ecx  
00A910B6  add         dword ptr [esp+eax*4+34h],edx  //Loop start
00A910BA  add         eax,1  
00A910BD  adc         ecx,edi  
00A910BF  jne         main+76h (0A910C6h)  
00A910C1  cmp         eax,6  
00A910C4  jb          main+66h (0A910B6h)  //Loop end

但似乎没有办法启用违反此规则的警告。

VC++ 10 enables the strict aliasing rule with /O1 and above. I use the test program(with count value 6) in chapter 'BENEFITS TO THE STRICT ALIASING RULE' of here. And get following asm code. You can see the load of b is done only once.

00A910AE  movzx       edx,word ptr [edx+2]  //Load of b
00A910B2  xor         eax,eax  
00A910B4  xor         ecx,ecx  
00A910B6  add         dword ptr [esp+eax*4+34h],edx  //Loop start
00A910BA  add         eax,1  
00A910BD  adc         ecx,edi  
00A910BF  jne         main+76h (0A910C6h)  
00A910C1  cmp         eax,6  
00A910C4  jb          main+66h (0A910B6h)  //Loop end

But looks like there isn't a way to enable the warning for breaking this rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文