[fpc-devel] Concatenating huge AnsiStrings

Sven Barth pascaldragon at googlemail.com
Fri Jun 28 14:09:17 CEST 2024


Virgo Pärna via fpc-devel <fpc-devel at lists.freepascal.org> schrieb am Fr.,
28. Juni 2024, 08:41:

> On Fri, 21 Jun 2024 20:03:56 +0200, Marco van de Voort via fpc-devel <
> fpc-devel at lists.freepascal.org> wrote:
> > Probably terminate with a heap out of memory error.
>
> Also depends of platform...
>
> program tests;
> var
>   s, s1, s2: ansistring;
> begin
>   SetLength(s1, High(int32));
>   SetLength(s2, High(int32));
>   WriteLn(Length(s1));
>   WriteLn(Length(s2));
>   s := s1 + s2;
>   WriteLn(Length(s));
> end.
>
> Works as 64 bit Windows program:
> 2147483647
> 2147483647
> 4294967294
>
> But gets "Runtime error 203 at $00404A01" on 32 bit Windows (out of
> memory).
>
> I did not actually know, that string length was 64 bits on 64 bit
> platform (at least on Windows), because even uint worked. uint32 version
> of same code generated warning at compile time. And uint32 version of
> same code results in strings with length 0. Because sizeint is signed.
>

The size of the dynamic string types and of dynamic arrays is SizeInt. Thus
it matches whatever the size of the underlying architecture is. Thus a
UInt32 can lead to problems (and warnings) on 32-bit systems, but not on
64-bit systems.


> I do not have access to 64 bit Delphi, but Embarcadero RAD Studio Athena
> documentation claims, that there ansistring length is limitud to "~2^31
> characters". And there is no implication, that 64 bit has different
> maximum.
>

It doesn't matter what Delphi does here. FPC had 64-bit support before
Delphi did and back then we picked the rule set that we thought was best.
Delphi also decided to follow the LP64 vs LLP64 rules regarding the Integer
type (it's 32-bit on 64-bit Windows, but 64-bit on 64-bit macOS and Linux)
while we decided to keep the size of Integer at 32-bit on 32- and 64-bit
systems.
These are differences that will stay this way.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20240628/10999bf3/attachment.htm>


More information about the fpc-devel mailing list