Recent

Author Topic: String truncates to the length of the first string in the array (compiler bug?)  (Read 1480 times)

stoffman

  • Jr. Member
  • **
  • Posts: 68
I'm using windows 10, Laz 2.2.4, FPC 3.2.2

In the first loop, it doesn't print 'abcde' but just 'abcd' , on the second loop it works correctly (see the attached image)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   s : string;
  4. begin
  5.   for s in ['1234','acbde'] do
  6.     OutputDebugString(PChar(s));
  7.  
  8.   for s in ['acbde','1234'] do
  9.     OutputDebugString(PChar(s));
  10. end;

I suspect this is a bug in the compiler. Where do I report this?


Thanks




TRon

  • Hero Member
  • *****
  • Posts: 3615
Trunk seem to have it fixed. I can verify the behaviour with the 3.2.2. compiler
This tagline is powered by AI

Thaddy

  • Hero Member
  • *****
  • Posts: 16132
  • Censorship about opinions does not belong here.
It is not a bug, but the casts to PChar!
Pascal allows #0 within a string, a PChar uses #0 as a string terminator. It is simply a silly programmer error. A Pascal string  <> a Pchar.
It borders at sheer stupidity. >:D >:D >:D or not reading manuals.

And for good measure, this has ALWAYS been the case. Nothing to do with any FreePascal version.

I have demonstrated that many times on this forum.
Code: Pascal  [Select][+][-]
  1. program crashtestdummy;
  2. const test:string = 'hello,'#0'world';
  3. begin
  4.   writeln(test);
  5.   writeln(pchar(test));
  6.   readln; // just for windows users
  7. end.

Sigh...
« Last Edit: June 17, 2023, 09:30:14 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

TRon

  • Hero Member
  • *****
  • Posts: 3615
It is not a bug, but the casts to PChar!
You seem to have had a different opinion on that same topic 6 years ago  :)
This tagline is powered by AI

VisualLab

  • Hero Member
  • *****
  • Posts: 568
I did a test too. I am attaching the source code and a screenshot of the console window. Let everyone check it out for themselves.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. const
  4.   SIndent = ' ->';
  5.  
  6. var
  7.   LText: String;
  8.   LArrayText1, LArrayText2: array [0..1] of String;
  9.   i: Integer;
  10.  
  11. begin
  12.   // using the array of strings given in the loop (for..in)
  13.   WriteLn('Using the array of strings given in the loop (for..in):');
  14.   WriteLn;
  15.   WriteLn('As Pascal string (numbers, text):');
  16.   for LText in ['1234', 'acbde'] do
  17.     WriteLn(SIndent + LText);
  18.   WriteLn;
  19.   WriteLn('As Pascal string (text, numbers):');
  20.   for LText in ['acbde', '1234'] do
  21.     WriteLn(SIndent + LText);
  22.   WriteLn;
  23.   WriteLn('As PChar (numbers, text):');
  24.   for LText in ['1234', 'acbde'] do
  25.     WriteLn(SIndent + PChar(LText));
  26.   WriteLn;
  27.   WriteLn('As PChar (text, numbers):');
  28.   for LText in ['acbde', '1234'] do
  29.     WriteLn(SIndent + PChar(LText));
  30.   // using the array of strings defined earlier (for..to..do)
  31.   WriteLn;
  32.   WriteLn('========');
  33.   WriteLn;
  34.   WriteLn('Using the array of strings defined earlier (for..to..do):');
  35.   WriteLn;
  36.   LArrayText1[0] := '1234';
  37.   LArrayText1[1] := 'acbde';
  38.   LArrayText2[0] := 'acbde';
  39.   LArrayText2[1] := '1234';
  40.   WriteLn('As Pascal string (numbers, text):');
  41.   for i := 0 to 1 do
  42.     WriteLn(SIndent + LArrayText1[i]);
  43.   WriteLn;
  44.   WriteLn('As Pascal string (text, numbers):');
  45.   for i := 0 to 1 do
  46.     WriteLn(SIndent + LArrayText2[i]);
  47.   WriteLn;
  48.   WriteLn('As PChar (numbers, text):');
  49.   for i := 0 to 1 do
  50.     WriteLn(SIndent + PChar(LArrayText1[i]));
  51.   WriteLn;
  52.   WriteLn('As PChar (text, numbers):');
  53.   for i := 0 to 1 do
  54.     WriteLn(SIndent + PChar(LArrayText2[i]));
  55.   // prevent the console window from closing
  56.   ReadLn;
  57. end.

---
Edit: I used Lazarus 2.2.6.
« Last Edit: June 17, 2023, 10:03:51 pm by VisualLab »

TRon

  • Hero Member
  • *****
  • Posts: 3615
This tagline is powered by AI

stoffman

  • Jr. Member
  • **
  • Posts: 68
It is not a bug, but the casts to PChar!
Pascal allows #0 within a string, a PChar uses #0 as a string terminator. It is simply a silly programmer error. A Pascal string  <> a Pchar.
It borders at sheer stupidity. >:D >:D >:D or not reading manuals.

And for good measure, this has ALWAYS been the case. Nothing to do with any FreePascal version.

I have demonstrated that many times on this forum.
Code: Pascal  [Select][+][-]
  1. program crashtestdummy;
  2. const test:string = 'hello,'#0'world';
  3. begin
  4.   writeln(test);
  5.   writeln(pchar(test));
  6.   readln; // just for windows users
  7. end.

Sigh...

Did you see the example? did you read the example? did you understand the example?

Obviously you failed at one of these tasks before answering. Please try better next time. 

jamie

  • Hero Member
  • *****
  • Posts: 6733
Oh, Thaddy, pay no mind to the shoot first and Oops, ask questions later guy!

Just noise in the background that gets attenuated with the Noise Blanker switch!
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
For heavens sake. It is fixed with commit https://gitlab.com/freepascal.org/fpc/source/-/commit/815734c47a4afe7d07083757f9ed6df233762abb

And it's also been merged to 3.2.3, so it will be in 3.2.4.

 

TinyPortal © 2005-2018