随笔 - 51  文章 - 0 评论 - 36 阅读 - 77579
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

两条函数:

procedure GetTexture(PTex: PTTexture);

procedure GetTexture(var PTex: PTTexture);

上面这两条函数有区别吗?有,而且不注意,问题大发了。一般来说,由于是指针,我们很少加特别的指示符。但是在使用中,会一不留神就会留下隐患。因为不好查。

复制代码
function XXXX.GetToImageTexture: PTTexture;
var
  nPTex: PTTexture;
begin
     Result := nil;
     GetTexture(nPTex);     //procedure GetTexture(PTex: PTTexture);
     if nPTex <> nil then Result := nPTex;
     nPTex := nil;
end;

//执行函数,返回值是nil;


function XXXX.GetToImageTexture: PTTexture;
var
  nPTex: PTTexture;
begin
     Result := nil;
     GetTexture(nPTex);     //procedure GetTexture(var PTex: PTTexture);PTTexture);
     if nPTex <> nil then Result := nPTex;
     nPTex := nil;
end;

//执行函数,返回值是正确的值!!!
复制代码

之所以出现这样的问题,是因为第一条函数传递的是值,而临时变量指针nPTex根本就没有分配内存,是没有值可以传递的。所以采用这种方式的前提是:指针是分配好内存的,或者说,已经指向一块内存区域。

而第二种却可以正常传递进去,因为传递的是地址,指针本身的地址,所以可以得到正确的结果,而不需要事先分配好内存。

留此记录备忘。

 

posted on   一笑如风  阅读(382)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示