W1024组合有符号和无符号类型-扩展了两个操作数 [英] W1024 Combining signed and unsigned types - widened both operands

查看:526
本文介绍了W1024组合有符号和无符号类型-扩展了两个操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止此警告?
[DCC警告] uFvSystem.pas(293):W1024组合有符号和无符号类型-扩展了两个操作数

 函数LinkerTimestamp:TDateTime;超载; 
开始
结果:= PImageNtHeaders(HInstance + PImageDosHeader(HInstance)^ ._ lfanew)
^ .FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
结尾;


解决方案

错误消息表明您正在使用混合有符号和无符号操作数。唯一的整数算法在这里:

  HInstance + PImageDosHeader(HInstance)^ ._ lfanew 

第一个操作数是无符号的,第二个操作数是有符号的,即使它必须是正数也是如此。



您可以通过强制转换来取消警告。最好在无符号上下文中执行算术运算,从而避免范围检查错误。因此,强制类型转换放置在第二个操作数周围:

  HInstance + NativeUInt(PImageDosHeader(HInstance)^ ._ lfanew)

  HInstance + Cardinal(PImageDosHeader(HInstance)^ ._ lfanew)

如果您的Delphi较旧具有 NativeUInt



但是,您实际上是在对指针执行算术运算,所以我会这样写:

  PByte(HInstance)+ PImageDosHeader(HInstance)^ ._ lfanew 

  PAnsiChar(HInstance)+ PImageDosHeader(HInstance)^ ._ lfanew 


PByte 不支持算术运算。


How can I prevent this warning? [DCC Warning] uFvSystem.pas(293): W1024 Combining signed and unsigned types - widened both operands

function LinkerTimestamp: TDateTime; overload;
begin
  Result := PImageNtHeaders(HInstance + PImageDosHeader(HInstance)^._lfanew)
   ^.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
end;

解决方案

The error message indicates that you are performing integer arithmetic with mixed signed and unsigned operands. The only integer arithmetic is here:

HInstance + PImageDosHeader(HInstance)^._lfanew

The first operand is unsigned, the second is signed, even though it must be positive.

You can suppress the warning with a cast. It is better to perform the arithmetic in an unsigned context and so avoid range check errors. Hence the cast is placed around the second operand:

HInstance + NativeUInt(PImageDosHeader(HInstance)^._lfanew)

or

HInstance + Cardinal(PImageDosHeader(HInstance)^._lfanew)

if you have an older Delphi that does not have NativeUInt.

However, you are actually performing arithmetic on pointers and so I would write it like this:

PByte(HInstance) + PImageDosHeader(HInstance)^._lfanew

or

PAnsiChar(HInstance) + PImageDosHeader(HInstance)^._lfanew

in older Delphi versions for which PByte does not support arithmetic.

这篇关于W1024组合有符号和无符号类型-扩展了两个操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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