Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-01

Length function on ansistrings

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Emerson

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?

Mark

Reply by email

Peter Vreman
to Mark Emerson
you wrote 8 lines
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.

Peter

Reply by email

Unknown author
to Peter Vreman
Peter Vreman wrote 12 lines
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.

Reply by email

Marco van de Voort
to Unknown author
Show quoted text 17 lines
On Sat, 4 Jan 2003, Peter Vreman wrote: > 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.

And note that it therefore may contain #0's (though whether that is smart is a different subject).

The #0 is for C compability, to allow casting to pchar.

Reply by email