[fpc-devel] SizeOf( const typcasted as shortstring)

Martin fpc at mfriebe.de
Mon Aug 13 02:32:01 CEST 2018


On 13/08/2018 00:12, Dmitry Boyarintsev wrote:
> On Sun, Aug 12, 2018 at 5:40 PM Bart <bartjunk64 at gmail.com 
> <mailto:bartjunk64 at gmail.com>> wrote:
>
>     const
>       x = ShortString('abc');
>     begin
>       writeln(SizeOf(x));
>     end.
>
>     Delphi (7) prints 256, fpc prints 3.
>
>     Is that a bug or an implementation detail?
>
> Implementation detail.
> And it seems to be wrong on Delphi size, because for reason it assigns 
> a type to a (non-typed) constant.
>
Interesting.

Yet the compiler still seems to add some type or at least type specific 
behaviour. (That alone is ok)

But if the first string has the length in a[0], then that constant has 6 
bytes of data. Yet sizeof only reports 5.
So what is the real size of this constant?


program Project1; {$H+}
const
   a = 'abcde';
   b = 'abcde 
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
begin
   writeln(SizeOf(a)); // 5
   writeln(SizeOf(b)); // 5
   writeln(byte(a[0]));  // no range check / shortstring has index 0 
with length
   writeln(byte(b[0])); // range check / not a shortstring
   readln;
end.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180813/d1d8ef50/attachment.html>


More information about the fpc-devel mailing list