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.