zoukankan      html  css  js  c++  java
  • System.New、System.Dispose 为某个指针申请和释放内存

    举例:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    implementation
    
    {$R *.dfm}
    
    Type
      TMyRec = record     {定义结构}
        name: string[8];
        age : Word;
      end;
      PMyRec = ^TMyRec;   {定义结构指针; Delphi 一般把它定义在结构前面, 都可以}
    
    
    {如果直接使用结构, 不使用指针}
    procedure TForm1.Button1Click(Sender: TObject);
    var
      r: TMyRec;
    begin
      r.name := '张三';
      r.age := 99;
      ShowMessage(Format('%s %d 岁', [r.name, r.age])); {张三 99 岁}
    end;
    
    {如果使用结构指针}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      pr: PMyRec;
    begin
      New(pr);            {分配内存}
      pr^.name := '张三';
      pr^.age := 99;
      ShowMessage(Format('%s %d 岁', [pr^.name, pr^.age])); {张三 99 岁}
      Dispose(pr);        {释放内存}
    end;
    
    {Delphi 允许使用结构时做如下简化}
    procedure TForm1.Button3Click(Sender: TObject);
    var
      pr: PMyRec;
    begin
      New(pr);
      pr.name := '张三';
      pr.age := 99;
      ShowMessage(Format('%s %d 岁', [pr.name, pr.age])); {张三 99 岁}
      Dispose(pr);
    end;
    
    end.
    
    System 单元下的公用函数目录

  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 状态压缩DP
    优先队列原理与实现【转】
    testC-I
    济南NOIP冬令营 选拔(select)
    P4747 D’s problem(d)
    P4746 C’s problem(c)
    P4745 B’s problem(b)
    P4744 A’s problem(a)
    [bzoj] 1004: [HNOI2008]Cards
    NOIP2013 表达式求值
  • 原文地址:https://www.cnblogs.com/del/p/1125718.html
Copyright © 2011-2022 走看看