Recent

Author Topic: [SOLVED] What's difference between TList and TObjectList?  (Read 491 times)

egsuh

  • Hero Member
  • *****
  • Posts: 503
[SOLVED] What's difference between TList and TObjectList?
« on: October 20, 2020, 04:28:45 am »
And TFPGList and TFPGObjectList?

As far as I know, what really matters to me is that I do not have to free the objects with TObjectList myself.  Are there any differences?
« Last Edit: October 21, 2020, 05:53:26 am by egsuh »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1053
  • Former Delphi 1-7, 10.2 User
Re: What's difference between TList and TObjectList?
« Reply #1 on: October 20, 2020, 04:43:13 am »
o Lazarus v2.1.0 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

PascalDragon

  • Hero Member
  • *****
  • Posts: 2405
  • Compiler Developer
Re: What's difference between TList and TObjectList?
« Reply #2 on: October 20, 2020, 09:42:14 am »
  • Classes.TList: a list of pointers. You are responsible of casting correctly and to correctly allocate/free them (e.g. if you simply cast from/to PtrUInt to store numeric values you don't need to do any allocater)
  • Contnrs.TObjectList: a list of TObject instances. Your are responsible of casting correctly and the list itself may free the instances upon deletion (and destruction of the list) if OwnsObjects is True
  • FGL.TFPGList<>: a generic list. You specialize it with the type you want, thus it is typesafe, and managed types (like strings and dynamic arrays) are freed correctly. Note: record types need to implement an =-operator.
  • FGL.TFPGObjectList<>: a generic list of TObject descendants. You specialize with a descendant of TObject, thus the list is type safe. It will also free the elements if OwnsObjects is set to True.
  • Generics.Collections.TList<>: similar to FGL.TFPGList, but Delphi compatible and records don't need to implement an =-operator.
  • Generics.Collections.TObjectList<>: similar to FGL.TFPGObjectList, but Delphi compatible

In general I nowadays suggest to use one of the generic lists as they're more typesafe. The Generics.Collections unit is provided with FPC only from 3.2.0 on, though it's also available externally for 3.0.4. I suggest to use the FGL classes if size is a concern (e.g. embedded applications on small systems) as the Generics.Collections are more heavyweight, but more versatile and - especially the dictionary classes - faster.
« Last Edit: October 21, 2020, 09:20:08 am by PascalDragon »

ASerge

  • Hero Member
  • *****
  • Posts: 1693
Re: What's difference between TList and TObjectList?
« Reply #3 on: October 20, 2020, 08:00:04 pm »
A few other lists are worth mentioning:
Classes.TFPList - The basic list of pointers. The lightest and fastest. Classes.TList uses it internally, adding code to track delete/insert events (at least for compatibility with Delphi). I've seen patches on Delphi that "remove" these tracking excesses from the TList to speed up. In FPC, this is not necessary.
Contnrs.TFPObjectList - inheritor from TFPList, while maintaining the same speed, similar in functionality to Contnrs.TObjectList. Some of the LCL and FCL components use it.

egsuh

  • Hero Member
  • *****
  • Posts: 503
Re: What's difference between TList and TObjectList?
« Reply #4 on: October 21, 2020, 05:53:00 am »
Thank you for all your kind advices. It's not easy to grasp at once. I'll think over them which one is best for my purposes.

 

TinyPortal © 2005-2018