[fpc-pascal]Length function on ansistrings

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Sat Jan 4 10:15:42 CET 2003


On Sat, 4 Jan 2003, Peter Vreman wrote:

> At 00:45 4-1-2003, you wrote:
> >Does the Length function applied to an ansistring return (1) the actual
> >length of the string (determined by the position of the null terminator)
> >or (2) the maximum length allocated for the string (recorded in a
> >longint just below the first character)?
> >
> >Someone told me the answer is (2) and to guarantee getting the actual
> >length, one must use Length (Pchar(s)), where s is an ansistring.  Is
> >this true?
>
> It returns the actual length. The maximumlength is only used for internal
> householding.

To clarify:  AnsiString is defined as a 'silent' pchar :
a pchar that points to :

  @-12 : Longint for maximum size;
  @-8  : Longint for actual size;
  @-4  : Longint for reference count;
  @    : String + Terminating #0;
  Pchar(Ansistring) is a valid typecast.

The length()  function returns the actual size of the string, at address
@-8. So strlen(pchar(s)) is not needed at all.

@-12 is the memory actually allocated on the heap for this string.

Michael.





More information about the fpc-pascal mailing list