zoukankan      html  css  js  c++  java
  • 指针传递的一些必要的记录,避免以后又忘记了。

    两条函数:

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

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

    留此记录备忘。

  • 相关阅读:
    进程间通信小结
    菜鸡和菜猫进行了一场Py交易
    菜鸡开始接触一些基本的算法逆向了
    菜鸡学逆向学得头皮发麻,终于它拿到了一段源代码
    静态分析-Windows找密码
    逆向-完成地址随机化关闭
    QSortFilterProxyModel 的过滤 排序
    linux命令2
    linux 命令1
    error c2059 c3905 c2148 c2238
  • 原文地址:https://www.cnblogs.com/GameDelphi/p/8595040.html
Copyright © 2011-2022 走看看