首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

发现:d2006的inline不是很可靠解决方法

2012-02-11 
发现:d2006的inline不是很可靠今天做一个函数发现一些怪事,通过调试发现,delphi2006的inline新语法在特定

发现:d2006的inline不是很可靠
今天做一个函数发现一些怪事,通过调试发现,delphi2006的inline新语法在特定情况下会出现错误。我做了个测试程序,不知道有没有搞错,大家鉴定鉴定,如果真的有问题,大家以后使用时要小心哟

///////////////////////////
//   project
///////////////////////////

program   inline_err;

{$APPTYPE   CONSOLE}

uses
    SysUtils,
    other_unit   in   'other_unit.pas ';

    procedure   normal(var   k:   my_type;   delta_x,   delta_y:   integer);
    //没有inline的函数
    begin
        inc(k.x1,   delta_x);
        inc(k.x2,   delta_x);

        inc(k.y1,   delta_y);
        inc(k.y2,   delta_y);
    end;

    procedure   compare(normal_r,   inlined_r:   my_type);
    //比较结果
    begin
        if   (normal_r.x1   <>   inlined_r.x1)
        or   (normal_r.x2   <>   inlined_r.x2)
        or   (normal_r.y1   <>   inlined_r.y1)
        or   (normal_r.y2   <>   inlined_r.y2)   then
            writeln( 'Error ')
        else
            writeln( 'Ok ');
    end;

    procedure   test_failed();
    var
        source,   normal_r,   inlined_r:   my_type;
    begin
        source.x1   :=   10;
        source.x2   :=   20;
        source.y1   :=   10;
        source.y2   :=   20;

        normal_r   :=   source;
        inlined_r   :=   source;

        normal(normal_r,   -normal_r.x1,   -normal_r.y1);
        inlined(inlined_r,   -inlined_r.x1,   -inlined_r.y1);

        compare(normal_r,   inlined_r);
    end;

    procedure   test_failed2();
    var
        source,   normal_r,   inlined_r:   my_type;
    begin
        source.x1   :=   10;
        source.x2   :=   20;
        source.y1   :=   10;
        source.y2   :=   20;

        normal_r   :=   source;
        inlined_r   :=   source;

        normal(normal_r,   -normal_r.x1,   -normal_r.y1);
        inlined2(inlined_r,   -inlined_r.x1,   -inlined_r.y1);

        compare(normal_r,   inlined_r);
    end;

    procedure   test_success();
    var
        source,   normal_r,   inlined_r:   my_type;
    begin
        source.x1   :=   10;
        source.x2   :=   20;
        source.y1   :=   10;


        source.y2   :=   20;

        normal_r   :=   source;
        inlined_r   :=   source;

        normal(normal_r,   -normal_r.x2,   -normal_r.y2);
        inlined(inlined_r,   -inlined_r.x2,   -inlined_r.y2);

        compare(normal_r,   inlined_r);
    end;


var
    s:   string;
begin
    {   TODO   -oUser   -cConsole   Main   :   Insert   code   here   }
    test_failed();
    test_failed2();
    test_success();

    writeln( 'press   ENTRY   to   continue.. ');
    readln(s);
end.


/////////////////////////////
///   other   unit
/////////////////////////////
unit   other_unit;

interface
uses
    SysUtils,   Types;

type
    my_type   =   record   //or   object
        x1,   y1,   x2,   y2:   integer;
    end;

procedure   inlined(var   k:   my_type;   delta_x,   delta_y:   integer);   inline;
procedure   inlined2(var   k:   my_type;   delta_x,   delta_y:   integer);   inline;

implementation

procedure   inlined(var   k:   my_type;   delta_x,   delta_y:   integer);
begin
    inc(k.x1,   delta_x);
    inc(k.x2,   delta_x);

    inc(k.y1,   delta_y);
    inc(k.y2,   delta_y);
end;

procedure   inlined2(var   k:   my_type;   delta_x,   delta_y:   integer);
begin
    k.x1   :=   k.x1   +   delta_x;
    k.x2   :=   k.x2   +   delta_x;

    k.y1   :=   k.y1   +   delta_y;
    k.y2   :=   k.y2   +   delta_y;
end;


end.

运行可以发现,inline版的函数没有正确被编译,产生这种情况的条件:
1.   inline函数在别的单元
2.   调用时,k.x1,k.y1作为delta_x,   delta_y的实际参数


[解决办法]
inline本来就不大可靠吧。不能依赖它。
[解决办法]
dcc32 18.0确实有这个问题。
dcc32 18.5已经修正了。

热点排行