[fpc-pascal] Why use pointers to arrays?

Andrew Brunner andrew.t.brunner at gmail.com
Mon Oct 12 19:38:29 CEST 2009


On Sun, Oct 11, 2009 at 5:25 PM, "Vinzent Höfler"
<JeLlyFish.software at gmx.net> wrote:
> Andrew Brunner <andrew.t.brunner at gmail.com>:
>
>> 1st benefit:
>>
>> declaring methods associated with classes before TIntArray needs to be
>> defined or declared.
>>   eg. procedure DoSomething(Var Data:TIntArray); vs (DataP:PIntArray);
>
> Huh? Is there any difference other than the second one can take a NIL value?

Read up on forward declarations.  The technique of declaring a typed
pointer to any data structure in FPC and Delphi was that you can use
it in fields and methods of objects and data structures w/o having to
actually have it fleshed out until later in the interface envelope of
the unit.   I've found that using forward declarations is a saving
grace with complicated systems.

>> Lastly, passing by reference rather than by value is much faster.
>
> That's what "const" has been invented for: Letting the compiler decide the most efficient way.

Oh I don't know about that one.  I'd like someone else to comment on
that.  Does using "const" prohibit copies of the memory.  I had memory
leaks with Delphi 5 or 6 doing this way back when and I attributed it
to trying to pass by reference using const... IMO it should do this
but may have had lead me to believe this is not the case...  But the
fact it that passing by reference is faster than passing by value...
That was my only point - which you made quite well.  As far as the
invention of const descriptor - I only use that for conventional
objects since dealing with pointers to data by const or by var is
trivial not organization or for maintanince or code review ;-)

> Not to mention, expressing your intent:
>
> You don't change it, say so: "const".
> You change it,       say so: "var".
> You initialize it,   say so: "out".
>
> If you just take the pointer no-one knows if the value is in/in out/or out. That's clearly not a "benefit".
>
Ahhh... The beauty of Pascal over C++ ;-)  Buy you're getting way to
particular man...  You know I've never had a need for Out save for
once. LOL.



More information about the fpc-pascal mailing list