Free pascal 指针处理

TList.Add() 参数是一个 Pointer, 下面是一个如何将 Integer 值处理为 Pointer的. 

copy from :    https://forum.lazarus.freepascal.org/index.php?topic=22905.0

复制代码
type
  pinteger=^integer;
var
  aList:TList;
  pi:pinteger;
  i:integer;
begin
  aList:=TList.Create;

  for i:=0 to 9 do
  begin
    New(pi);
    pi^:=i;
    aList.Add(pi);
  end;

  for i:=0 to aList.Count-1 do
    writeln(pinteger(aList[i])^);

  //take care about self created items
  for i:=aList.Count-1 downto 0 do
    Dispose(pinteger(aList[i]));

  aList.Clear;
  aList.Free;
end.
复制代码

 

使用自定义对象, 比直接使用Pointer更简单.

复制代码
type
  TTest=class
    Value:integer;
  end;

var
  aList:TList;
  aTest:TTest;
  i:integer;
begin
  aList:=TList.Create;

  for i:=0 to 9 do
  begin
    aTest:=TTest.Create;
    aTest.Value:=i;
    aList.Add(aTest);
  end;

  for i:=0 to aList.Count-1 do
    writeln(TTest(aList[i]).Value);

  //take care about self created items
  for i:=aList.Count-1 downto 0 do
    TTest(aList[i]).Free;

  aList.Clear;
  aList.Free;
end.
复制代码

 

posted @   harrychinese  阅读(287)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2011-11-03 使用python开发命令行程序的知识点
2011-11-03 cx_Oracle模块介绍
2011-11-03 python的数据库模块
2011-11-03 将Komodo Edit打造成Python开发的IDE
点击右上角即可分享
微信分享提示