为什么#pragma优化(“,",关闭) [英] Why #pragma optimize("", off)

查看:468
本文介绍了为什么#pragma优化(“,",关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在审查C ++ MFC项目.在某些文件的开头有以下行:

I'm reviewing a C++ MFC project. At the beginning of some of the files there is this line:

#pragma optimize("", off)

我知道这将关闭所有以下功能的优化.但是这样做的动机通常是什么?

I get that this turns optimization off for all following functions. But what would the motivation typically be for doing so?

推荐答案

我看到了正确的生产代码,但是它太复杂了,以至于使优化器产生错误的输出.这可能是关闭优化的原因.

I've seen production code which is correct but so complicated that it confuses the optimiser into producing incorrect output. This could be the reason to turn optimisations off.

但是,我认为代码更容易出错,具有未定义的行为.优化器暴露了这一点,并导致错误的运行时行为或崩溃.没有优化,代码就会起作用".而不是发现并消除潜在的问题,而是通过禁用优化并将其留在那儿来修复"该问题.

However, I'd consider it much more likely that the code is simply buggy, having Undefined Behaviour. The optimiser exposes that and leads to incorrect runtime behaviour or crashes. Without optimisations, the code happens to "work." And rather than find and remove the underlying problem, someone "fixed" it by disabling optimisations and leaving it at that.

当然,这与脆弱和解决方法有关.新硬件,新OS补丁,新编译器补丁,其中任何一个都可能破坏这样的修复".

Of course, this is about as fragile and workarounds can get. New hardware, new OS patch, new compiler patch, any of these can break such a "fix."

即使是因为第一个原因存在杂物,也应将其记录在案.

Even if the pragma is there for the first reason, it should be heavily documented.

这篇关于为什么#pragma优化(“,",关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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