[fpc-pascal]Classes/Objects/Pointers / Pointer Help
tom_at_work at yline.com
Mon Feb 10 18:37:46 CET 2003
From: "Matt Emson" <memsom at interalpha.co.uk>
Sent: Monday, February 10, 2003 3:41 PM
Subject: RE: [fpc-pascal]Classes/Objects/Pointers / Pointer Help
> RE: Dynamic arrays, What about using the Tcollection? Is there any
> dis-advantage? I know a lot of TP Programmers swear by them.
No, if you mean the dynamic array stuff I used in my example. TP's
TCollection (the one objects unit) works internally exactly like shown here,
it just provides an OOP wrapper above an internal array of pointers for
initialization, finalization, access and management - with all its
advantages and disadvantages (e.g. performance, convenience, ...).
Compared to the Delphi style dynamic arrays (the terms
'object'/'class'/'dynamic array' have been a little bit overused in this
thread lately imo :) following things could be interpreted as advantages
(mostly syntactical stuff):
No method invocations by hand if any, you can use the (imo) more convenient
brackets compared to calls. You get rid of the (ugly) '^' - character. It is
more convenient (imo): Memory is automatically freed when dynamic array is
set to nil or not used anymore. It is reference counted like ansistrings.
You still have something like a 'constructor' though (SetLength() method).
There are a few helper functions (like Copy(), Slice(), ...) for easy
handling too. Regarding Performance: range checks and especially the array
access etc. could be inlined, saving an indirect call for every access.
[Don't know the FPC implementation]. There is partial compatibility to fixed
size arrays (e.g. when passing arrays as parameters to methods).
One (little) drawback: you should be aware that a variable of the kind of
dynamic array is actually a pointer and not the real thing. Sometimes causes
hard to trace problems if you simply replace all static arrays with dynamic
ones in your code... ;-)
Probably I forgot one or two things, but it's quite a nice thing imo.
More information about the fpc-pascal