zoukankan      html  css  js  c++  java
  • Delphi用地址访问相关的内存(此处数字要为有效地址,且知道该地址的类型)

    -----------Delphi XE---

    仅仅是学习记录

    ---------------

    ------------unit 开始

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Edit1: TEdit;
    Button3: TButton;
    Label1: TLabel;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    Myarray:array of string;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    type
    TArec=^Arec;
    Arec=record
    Name:string;//string[8];
    Age:integer;
    end;
    var
    p: ^Arec;
    i,Num: Integer;
    P001:Pointer;
    type
    ArrPoint = array of Arec;
    begin

    {

    GetMemory申请的内存一定要初始化(可以用FillChar),申请的内存不初始化,这段没有初始化的内存只是暂时属于你,很快就可能被别人拿走,
    还有就是,没有初始化的内存,很有可能里面就有残留的数据,为此申请内存一定要初始化,不要说什么现在也没有问题啊,申请内存不初始化等于
    在制造BUG

    }

    Num:=4;//当前Name:string;Num大于5就不会报错 //Name:string[8]; 这样的话,4是不会报错的
    p := GetMemory(Num * SizeOf(Arec));
    Label1.Caption:=IntToStr(Num * SizeOf(Arec));
    FillChar(p^, num*sizeof(arec), #0); //当前Name:string;Num=4时,这么写不会报错
    for i := 0 to Num-1 do
    begin
    P001:=pinteger(Integer(ArrPoint(p))+8*i);
    Memo2.Lines.Add(IntToStr(integer(@(ArrPoint(p)[i]))) +' '+inttostr(Integer(ArrPoint(p))+8*i));
    ArrPoint(p)[i].Name := '张三'+inttostr(i);
    self.Memo1.Lines.Add(ArrPoint(p)[i].Name+',年龄:'+inttostr(ArrPoint(p)[i].Age));
    ShowMessage(TArec(p001).Name);
    ArrPoint(p)[i].Age :=i+30;
    end;
    FreeMemory(p);
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    var
    i:Integer;
    begin
    SetLength(Myarray,StrToIntDef(Edit1.Text,4));
    if StrToIntDef(Edit1.Text,4)<5 then
    begin
    for I := Low(Myarray) to High(Myarray) do
    Myarray[i]:='Str_'+IntToStr(i)
    end
    else
    begin
    Myarray[0]:='88';
    end;
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    Type
    TArr<MT> = array[0..9] of MT; {定义一个泛型数组}
    var
    Arr: TArr<Integer>;
    i: Integer;
    begin
    for i := Low(Arr) to High(Arr) do
    Arr[i] := i * i;

    Memo1.Clear;
    for i := Low(Arr) to High(Arr) do
    Memo1.Lines.Add(Format('Arr[%d] = %d', [i, Arr[i]]));
    end;

    end.

    ----------------unit 结束

    -----------Form 开始

    object Form1: TForm1
    Left = 1113
    Top = 270
    Caption = 'Form1'
    ClientHeight = 332
    ClientWidth = 420
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    object Label1: TLabel
    Left = 103
    Top = 40
    Width = 105
    Height = 13
    AutoSize = False
    Caption = 'Label1'
    end
    object Button1: TButton
    Left = 103
    Top = 112
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Memo1: TMemo
    Left = 0
    Top = 8
    Width = 97
    Height = 161
    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    Lines.Strings = (
    'Memo1')
    TabOrder = 1
    end
    object Button2: TButton
    Left = 34
    Top = 280
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
    end
    object Edit1: TEdit
    Left = 8
    Top = 237
    Width = 121
    Height = 21
    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    TabOrder = 3
    Text = '4'
    end
    object Button3: TButton
    Left = 288
    Top = 192
    Width = 75
    Height = 25
    Caption = #27867#22411
    TabOrder = 4
    OnClick = Button3Click
    end
    object Memo2: TMemo
    Left = 214
    Top = 8
    Width = 179
    Height = 161
    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    Lines.Strings = (
    'Memo2')
    TabOrder = 5
    end
    end

    ------------Form 结束

    参考博客

    https://q.cnblogs.com/q/54116/

  • 相关阅读:
    HttpEntity转换Inputstream(红色)加XmlPull解析
    ImageLoader加载图片
    HttpClient——Get,Post
    Android例子源码非第三方实现根据字母排序的城市列表
    Android 仿QQ消息界面
    css折叠表格
    前端页面文字长度显示控制
    (首页上一页下一页尾页 + 下拉框跳转)分页功能
    基于bootstrap的分页插件
    HTML 禁止复制文字
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14539540.html
Copyright © 2011-2022 走看看