分配基本类型和结构时的近期奇怪行为 [英] Recent strange behavior when assigning primitive types and structs

查看:81
本文介绍了分配基本类型和结构时的近期奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个运行了几个月的应用程序,直到最近.由于某些奇怪的原因,基本赋值语句在看似随机的地方返回了错误的结果.

We have an application that has worked fine for several months, until just recently. For some strange reason, basic assignment statements were returning incorrect results in seemingly random places.

首先,分配返回的整数是错误的(即int x = myClassInstance.getX();).分配后,结果将向左移2个字节,并补零.

First, assigning a returned integer was incorrect (i.e. int x = myClassInstance.getX(); ). The assignment would yield a result shifted left by 2 bytes and zero padded.

第二,分配返回的结构不正确(即MyStruct s = myClassInstance.getS();).该分配将产生一个结构,该结构的成员不正确,但偏移2个字节.

Second, assigning a returned struct was incorrect (i.e. MyStruct s = myClassInstance.getS(); ). The assignment would yield a struct whose members were incorrect by an offset of 2 bytes.

我们将问题追溯到预处理器指令,以对结构类型(即#pragma pack(2))强制使用2个字节的边界.通过删除此指令,并将其替换为编译器字节对齐标志(/Zp),我们解决了该问题.

We traced the problem back to our preprocessor directive to enforce a 2 byte boundary on our struct types (i.e. #pragma pack(2) ). By removing this directive, and replacing it with the compiler byte alignment flag ( /Zp ), we resolved the issue.

我的问题是,在过去应用程序运行正常之后,为什么现在应该出现问题?我了解字节对齐可能是一个安全漏洞,所以我目前的理论是,最近的Windows更新可以解决此漏洞, 但会破坏我们的应用程序.

My question is, why should this present a problem now, after the application has been working fine in the past? I understand that byte alignment can be a security vulnerability, so my current theory is that a recent windows update resolves a vulnerability, but breaks our application.

我们已经在Windows 7和XP(同样不要问)的Visual Studio 2010和6中测试了此问题和解决方案(不要问).在所有情况下,问题都得到了复制和解决.

We have tested this problem, and resolution, in both Visual Studio 2010 and 6 (don't ask) for both Windows 7 and XP (again, don't ask). In all cases, the problem was replicated and resolved.

在此先感谢您提供任何见解或指导.

Thanks in advance for any insights or guidance.

推荐答案


我的问题是,在应用程序过去运行良好之后,为什么现在应该出现问题?
My question is, why should this present a problem now, after the application has been working fine in the past?

您最近是否更改了MyStruct的结构?您是否将对齐方式设置为"2"?是明智的项目,还是在块/文件中?

向我们展示MyStruct的结构和方法getX(),getS().

Did you change the structure MyStruct in recent times? Are you setting the alignment to "2" project wise, or in a block/file?

Show us the structure MyStruct and methods getX(), getS().


这篇关于分配基本类型和结构时的近期奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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