Bug in typecasting integer to array of characters lazarus / 62-General ---------------------------------------------------------------------- Avinash · 2023-11-17 10:24 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  LongRec = record    case byte of      0: (Lo, Hi: Word);      1: (Chars4: array [0..3] of Char);  end; var I: LongInt;    S: String;begin   S := 'test';   I := $5A5A5A5A;  {'ZZZZ'}  WriteLn(LongRec(I).Chars4 + S);   I := $5A5A5A;    {'ZZZ'#0}  WriteLn(LongRec(I).Chars4 + S);   I := $5A5A;      {'ZZ'#0#0}  WriteLn(LongRec(I).Chars4 + S);   I := $5A;        {'Z'#0#0#0}  WriteLn(LongRec(I).Chars4 + S);   I := 0;          {#0#0#0#0}  WriteLn(LongRec(I).Chars4 + S);end. Output by Borland TP (correct): QuoteZZZZtest ZZZ test ZZ  test Z   test     test Output by FPC 3.2.2 Win32 (incorrect): QuoteZZZZtest ZZZtest ZZtest  Ztest  test ---------------------------------------------------------------------- Fibonacci · 2023-11-17 10:34 I dont think its a bug, I would expect the output to look like it is looking. #0 is char array terminator, casted to string, then "s" appended, looks ok. ---------------------------------------------------------------------- marcov · 2023-11-17 11:05 It might not even be Free Pascal, but merely the change of OS output functionality. TP might even differ between using Crt and not. ---------------------------------------------------------------------- Avinash · 2023-11-17 11:18 The output has nothing to do with it, it was originally discovered in a binary context. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  LongRec = record    case byte of      0: (Lo, Hi: Word);      1: (Chars4: array [0..3] of Char);  end; var I: LongInt;    S: String;begin  I := $5A5A5A5A; {'ZZZZ'}  S := LongRec(I).Chars4;  WriteLn(Length(S):2,' ',S);   I := $5A00005A; {'Z'#0#0'Z'}  S := LongRec(I).Chars4;  WriteLn(Length(S):2,' ',S);end. FPC 3.2.2 Win32: Quote  4 ZZZZ  1 Z ---------------------------------------------------------------------- Fibonacci · 2023-11-17 11:25 So whats wrong? $5A00005A as PChar casted to String will have length of 1 ---------------------------------------------------------------------- marcov · 2023-11-17 11:52 I think his beef is that pchar automated conversions are afaik a Delphi thing. ---------------------------------------------------------------------- Avinash · 2023-11-17 12:18 Discovered thing: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var Q0: array [0..3] of Char;    Q1: array [1..4] of Char;begin  Q0 := 'Z'#0#0'Z';  Q1 := 'Z'#0#0'Z';  WriteLn(Q0);  WriteLn(Q1);end. Output: QuoteZ  Z  Z ---------------------------------------------------------------------- Fibonacci · 2023-11-17 12:40 Is it a bug if its done on purpose? zerobased = as PAnsiChar (to first nil) otherwise = print full array Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of ansichar; zerobased: boolean = true); iocheck; compilerproc;var  ArrayLen : longint;  p : pansichar;Begin  If (InOutRes<>0) then   exit;  case TextRec(f).mode of    fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:      begin      p:=pansichar(@s);        if zerobased then          begin            { can't use StrLen, since that one could try to read past the end }          { of the heap (JM)                                              }            ArrayLen:=IndexByte(p^,high(s)+1,0);            { IndexByte returns -1 if not found (JM) }            if ArrayLen = -1 then              ArrayLen := high(s)+1;          end        else        ArrayLen := high(s)+1;        If Len>ArrayLen Then          fpc_WriteBlanks(f,Len-ArrayLen);        fpc_WriteBuffer(f,p^,ArrayLen);      end;    fmInput: InOutRes:=105  else InOutRes:=103;  end;End; PS. Thats something different than your first post ---------------------------------------------------------------------- Warfley · 2023-11-17 13:34 This is at most a bug in the implementation of the write intrinsic, and even then I would not be so sure about that. Static char Arrays are often used to interface with C apis such as Berkeley sockets. C strings are #0 terminated, therefore treating static char Arrays as C Strings actually makes quite some sense. ---------------------------------------------------------------------- nanobit · 2023-11-17 14:49 Quote from: Fibonacci on November 17, 2023, 01:40:19 pmzerobased = as PAnsiChar (to first nil) otherwise = print full array This is by design. Only zero-based array is compatible with PChar. Then string( array) is like string( pchar). This is also Delphi compatible.