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

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

    留此记录备忘。

  • 相关阅读:
    Splashtop :符合 HIPAA 标准的远程桌面软件
    学生如何在家中访问学校许可的软件
    Splashtop用于远程实验室的功能得到增强
    docker环境安装,镜像和容器常用命令
    vue-cli入门
    webpack快速入门
    Vue路由vue-router
    Vue组件化
    Vue指令
    Vue实例
  • 原文地址:https://www.cnblogs.com/GameDelphi/p/8595040.html
Copyright © 2011-2022 走看看