指针传递的一些必要的记录,避免以后又忘记了。

两条函数:

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根本就没有分配内存,是没有值可以传递的。所以采用这种方式的前提是:指针是分配好内存的,或者说,已经指向一块内存区域。

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

留此记录备忘。

原文地址:https://www.cnblogs.com/GameDelphi/p/8595040.html