编译器为什么会跳过变量赋值?

7

I have the following procedure :

procedure GetDegree(const num : DWORD ; var degree : DWORD ; min ,sec : Extended);
begin
  degree := num div (500*60*60);
  min := num div (500*60) - degree *60;
  sec := num/500 - min *60 - degree *60*60;
end;

在degree变量被赋值后,调试器会跳到过程的末尾。为什么会这样?

1个回答

17

这是一种优化。变量minsec是按值传递的。这意味着对它们的修改对调用者不可见,并且仅在此过程中私有。因此,编译器可以确定将它们赋值是没有意义的。分配给变量的值永远不会被读取。所以编译器选择节省时间并跳过这些赋值操作。 我认为你应该像这样声明该过程:

procedure GetDegree(const num: DWORD; var degree: DWORD; var min, sec: Extended);

就像我在你之前的问题中所说的,使用 Extended 没有太大意义。您最好使用标准浮点类型之一,Single 或者 Double。或者甚至使用泛型 Real,它映射到 Double

此外,您已将 min 声明为浮点类型,但计算结果是整数。关于这一点,我的回答对您之前的问题非常精确。


我建议您创建一个记录来保存这些值。传递三个单独的变量会使您的函数接口非常混乱,并破坏封装性。只有将这三个值作为一个整体考虑时才具有意义。

type
  TGlobalCoordinate = record
    Degrees: Integer;
    Minutes: Integer;
    Seconds: Real;
  end;

function LongLatToGlobalCoordinate(const LongLat: DWORD): TGlobalCoordinate;
begin
  Result.Degrees := LongLat div (500*60*60);
  Result.Minutes := LongLat div (500*60) - Result.Degrees*60;
  Result.Seconds := LongLat/500 - Result.Minutes*60 - Result.Degrees*60*60;
end;

function GlobalCoordinateToLongLat(const Coord: TGlobalCoordinate): DWORD;
begin
  Result := Round(500*(Coord.Seconds + Coord.Minutes*60 + Coord.Degrees*60*60));
end;

再次感谢你今天的巨大帮助,David。我之前不知道这些该死的优化,我能从代码中删除它们吗? - opc0de
6
你不想移除优化。你需要修复你的代码,以便将“分(min)”和“秒(sec)”值返回给调用者。否则,为什么要在第一次计算中费心计算它们呢? - David Heffernan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接