[fpc-pascal] Pchar from constant string

Michael Van Canneyt michael at freepascal.org
Thu Jun 29 16:31:14 CEST 2023



On Thu, 29 Jun 2023, Mattias Gaertner via fpc-pascal wrote:

> On Thu, 29 Jun 2023 21:18:44 +0700
> Hairy Pixels via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>
>> What is really happening in this snippet? I think it must be
>> implicitly taking the address of the constant string but is it also
>> adding the null terminator automatically? The string prints with
>> writeln so it must be null terminated right?
>>
>> var
>>   p: Pchar;
>> begin
>>   p := '123';
>>   writeln(p);
>
> AFAIK the trailing #0 is already stored in the binary.

Yes, it is:

---
_$PROGRAM$_Ld1:
# [4] p := '123';
         .ascii  "123\000"
.Le11:
---

Just as it is for a shortstring and ansistring:
---
# [6] s:='456';
         .ascii  "\003456\000"
.Le12:
         .size   _$PROGRAM$_Ld2, .Le12 - _$PROGRAM$_Ld2
---

Michael.


More information about the fpc-pascal mailing list