简体   繁体   English

移植到64位时如何避免size_t转换为int警告?

[英]How to avoid size_t to int casting warnings when porting to 64 bit?

I have to convert a very large C++ legacy code base to 64 bits. 我必须将非常大的C ++传统代码库转换为64位。 I've managed to get one of the base modules to compile, but even in that small module I get 800 warnings of: 我设法获得了要编译的基本模块之一,但是即使在这个小模块中,我也会收到800条警告:

warning C4267: = conversion from size_t to int, possible loss of data

I understand why these appear, but what are my options for getting rid of them? 我知道为什么会出现这些,但是要摆脱它们我有什么选择? Is there any systematic way that avoids touching every single instance? 有没有什么系统的方法可以避免碰到每个实例?

One option is to disable the "loss of data" warning. 一种选择是禁用“数据丢失”警告。 To limit the effect of disabling the warning, MS Visual Studio has push and pop directives: 为了限制禁用警告的效果,MS Visual Studio具有pushpop指令:

#pragma warning(push)
#pragma warning(disable: 4267)
// legacy code
#pragma warning(pop)
// normal code

These #pragma directives are specific to Visual Studio; 这些#pragma伪指令特定于Visual Studio; you might want to wrap them with #ifdef _MSC_VER . 您可能想用#ifdef _MSC_VER来包装它们。

This is thought. 这是思想。 I'm pretty sure +90% of those warning can be ignored. 我敢肯定,这些警告中的+ 90%可以忽略。 I had similar problem and lots of warning on something like this: 我有类似的问题,并且对以下内容有很多警告:

sumeType tab[10];
int items = std::size(tab);
// or
functionWhichExeptsInt(std::size(tab))

In above example since std::size is a constexpr compiler could just detect that size value is small enough to fit into int so it should not report an warring but it does. 在上面的示例中,由于std::sizeconstexpr编译器可以检测到size值足够小以适合int因此它不应报告warring,但可以。

Problem is that there might be a cases where this warning can detect a real issue. 问题是,在某些情况下此警告可以检测到实际问题。 So disabling this warning is not a good approach. 因此,禁用此警告不是一个好方法。

In my project we have decided to keep warring, but do not threat it as an error: 在我的项目中,我们决定继续交战,但不要将其视为错误:

  • we reviewed them quickly, if something could be fixed by minimum change we did that 我们很快对其进行了审核,如果可以通过最小的更改来解决某些问题,我们会这样做
  • when required change was more complex, we just estimated potential danger of having a bug (after all we changed app from 32 to 64 bits to gain access to more memory). 当所需的更改更加复杂时,我们只是估计存在错误的潜在危险(毕竟,我们将应用程序从32位更改为64位以获得对更多内存的访问权限)。 If didn't see a risk, we just ignore it for now 如果没有发现风险,我们暂时将其忽略
  • we fix remaining warnings as code changes and we do not rash to fix them all now. 我们会在代码更改时修复剩余的警告,并且我们现在也不会急于将其全部修复。

This is more like mental problem: "Can I ignore those +100 warning for now?". 这更像是精神问题:“我现在可以忽略那些+100的警告吗?”。 I also love code without warnings reported, but some times it is better to live with them. 我也喜欢没有警告的代码,但有时最好与它们一起生活。

IMO this is more safe approach. IMO这是一种更安全的方法。

To search and eliminate bugs, occurring when porting a system from 32-bit to 64-bit, it's rational to use the PVS-Studio specialized tool. 要搜索并消除在将系统从32位移植到64位时出现的错误,使用PVS-Studio专用工具是合理的。 It is a static code analyzer which has a certain set of diagnostics ( Diagnosis of 64-bit errors ). 它是一种静态代码分析器,具有一组特定的诊断( 诊断64位错误 )。 Read here about issues when porting. 在此处阅读有关移植时的问题。

The analyzer will also issue V103 warnings when size_t is implicitly cast to int . size_t隐式转换为int时,分析器还将发出V103警告。 But unlike the compiler it does it in a smarter way. 但是与编译器不同的是,它以更智能的方式进行处理。 It doesn't complain about everything. 它并没有抱怨一切。 If it is aware that the range of the source value is small, it will be quiet. 如果知道源值的范围很小,它将很安静。

Example: 例:

for (size_t i = 0; i < 10; i++)
{
  int x = i; // i == [0..9], OK!
  //....
}

Yes, false positives will still occur, but there will be much less of them. 是的,误报仍会发生,但数量会少得多。 In addition, the analyzer provides a large number of options to suppress false positives. 此外,分析仪提供了大量选项来抑制误报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM