Lazarus

Free Pascal => Beginners => Topic started by: JMarques on March 30, 2023, 11:54:58 pm

Title: Variables as indexes of arrays (Solved)
Post by: JMarques on March 30, 2023, 11:54:58 pm
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12, compiler version 3.2.2

In this very simple program, row[1][0] is made equal to triad[0], which is 1. But when I ask the value of row[j][0] for j=1, I get the value 0. The problem appears also for other values of j and for the other members of row[j], but I believe that just concentrating in this fact may help to find the solution. Thanks for any help!

Code: Pascal  [Select][+][-]
  1. type
  2.   Triads = Array [0..2] of Integer;
  3. var
  4.   row: Array [0..3] of Triads;
  5.   j: Integer;
  6. Const  
  7.   triad: Triads = (1, 2, 3);
  8. Begin
  9. row[1][0] := triad[0];
  10. Writeln (row[1][0]);
  11. j:=1;
  12. Writeln (row[j][0]);
  13. Readln;
  14. End.
  15.  

Title: Re: Variables as indexes of arrays
Post by: KodeZwerg on March 31, 2023, 12:04:50 am
Code: Pascal  [Select][+][-]
  1. type
  2.   Triads = Array [0..2] of Integer;
  3. var
  4.   row: Array [0..3] of Triads;
  5.   i: Integer;
  6. Const
  7.   triad: Triads = (1, 2, 3);
  8. Begin
  9. row[1][0] := triad[0];
  10. Writeln (row[1][0]);
  11. i:=1;
  12. Writeln (row[0]);
  13. Readln;
  14. End.
What do you expect to get by doing:
Code: Pascal  [Select][+][-]
  1. Writeln (row[0]);
row[0] is no qualified number for writeln at all, it is part of Triads but not a number...
Title: Re: Variables as indexes of arrays
Post by: KodeZwerg on March 31, 2023, 12:07:15 am
( I attached a text file with the program because it doesn't appear correctly in this box.)
Please use on forum topic editor the [ # ] button to generate [ code=pascal ][ /code ] and put your code inside the ][
Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 12:17:07 am
What do you expect to get by doing:
Code: Pascal  [Select]
Writeln (row[0]);

In fact it is WriteLn (row[j][0]); as appears in the code I post just now.
Title: Re: Variables as indexes of arrays
Post by: Fred vS on March 31, 2023, 12:25:45 am
In fact it is WriteLn (row[j][0]); as appears in the code I post just now.

With the code that you just posted:

Code: Pascal  [Select][+][-]
  1. program triad_test;
  2.     type
  3.       Triads = Array [0..2] of Integer;
  4.     var
  5.       row: Array [0..3] of Triads;
  6.       j: Integer;
  7.     const  
  8.       triad: Triads = (1, 2, 3);
  9.   begin
  10.     row [1][0] := triad[0];
  11.     Writeln (row[1][0]);
  12.     j:=1;
  13.     Writeln (row[j][0]);
  14.     Readln;
  15.  end.

Code: Bash  [Select][+][-]
  1.  > /home/fred/fpc_test/triad_test

gives this on with fpc 3.2.2 Linux 64 bit (not tested on Windows 32):

Code: Bash  [Select][+][-]
  1. 1
  2. 1

[EDIT] Tested with fpc.exe 3.2.2 32 bit on Windows 11 and same result.
Title: Re: Variables as indexes of arrays
Post by: KodeZwerg on March 31, 2023, 12:30:41 am
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   Triads = array [0..2] of integer;
  5. var
  6.   row: array [0..3] of Triads;
  7.   i: integer;
  8. const
  9.   triad: Triads = (1, 2, 3);
  10. begin
  11.   // fill all array elements with values
  12.   row[0][0] := triad[0];
  13.   row[0][1] := triad[1];
  14.   row[0][2] := triad[2];
  15.   row[1][0] := triad[0];
  16.   row[1][1] := triad[1];
  17.   row[1][2] := triad[2];
  18.   row[2][0] := triad[0];
  19.   row[2][1] := triad[1];
  20.   row[2][2] := triad[2];
  21.   row[3][0] := triad[0];
  22.   row[3][1] := triad[1];
  23.   row[3][2] := triad[2];
  24.   // loop thru all values inside array
  25.   for i := Low(row[0]) to High(row[0]) do
  26.     WriteLn(row[0][i]);
  27.   for i := Low(row[1]) to High(row[1]) do
  28.     WriteLn(row[1][i]);
  29.   for i := Low(row[2]) to High(row[2]) do
  30.     WriteLn(row[2][i]);
  31.   for i := Low(row[3]) to High(row[3]) do
  32.     WriteLn(row[3][i]);
  33.   Readln;
  34. end.
Does that help/answer your question?
Title: Re: Variables as indexes of arrays
Post by: KodeZwerg on March 31, 2023, 12:34:30 am
Or more compact:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   Triads = array [0..2] of integer;
  5. var
  6.   row: array [0..3] of Triads;
  7.   i, ii: integer;
  8. const
  9.   triad: Triads = (1, 2, 3);
  10. begin
  11.   for i := Low(Row) to High(Row) do
  12.     for ii := Low(Row[i]) to High(Row[i]) do
  13.       Row[i][ii] := triad[ii];
  14.   for i := Low(Row) to High(Row) do
  15.     for ii := Low(Row[i]) to High(Row[i]) do
  16.       WriteLn(Row[i][ii]);
  17.   Readln;
  18. end.
Title: Re: Variables as indexes of arrays
Post by: jamie on March 31, 2023, 02:16:54 am
Quote
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12.

 did you notice the version number? Isn't that quite old and what version fpc was used for that back then ?
Title: Re: Variables as indexes of arrays
Post by: dseligo on March 31, 2023, 02:27:00 am
Quote
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12.

 did you notice the version number? Isn't that quite old and what version fpc was used for that back then ?

I also have this version, I think it's current one. My has FPC version 3.2.2.
Title: Re: Variables as indexes of arrays
Post by: jamie on March 31, 2023, 02:43:05 am
current release is 2.2.6 for  the IDE and 3.2.2 comes with it believe.

I started Lazarus I think when it was 1.64.x something way back.....
Maybe it's a typo but also maybe it's accurate, too?
Title: Re: Variables as indexes of arrays
Post by: dsiders on March 31, 2023, 03:37:52 am
current release is 2.2.6 for  the IDE and 3.2.2 comes with it believe.

I started Lazarus I think when it was 1.64.x something way back.....
Maybe it's a typo but also maybe it's accurate, too?

They're referring to the version number for fp.exe - the character mode IDE in FPC.
Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 11:00:19 am
In fact it is WriteLn (row[j][0]); as appears in the code I post just now.

gives this on with fpc 3.2.2 Linux 64 bit (not tested on Windows 32):

Code: Bash  [Select][+][-]
  1. 1
  2. 1

[EDIT] Tested with fpc.exe 3.2.2 32 bit on Windows 11 and same result.

Thanks for testing. That is the expected result, and I dont understand why it doesn't show in my system. I have been compiling much more complex programs in it without any issue. I wonder what can be happening.
Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 11:12:27 am
Or more compact:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.  

Thank you for your attention. I ran the program in the long version and got a correct result. With the compact version I got the following result, instead of the expected one. I see that only the compact version uses a variable for the first index of "row", and that is exactly where the problem lies.

Running "f:\zprograms\m2\project.exe "
1
1
1
3
0
0
0
0
0
0
3
0
Title: Re: Variables as indexes of arrays
Post by: Nitorami on March 31, 2023, 12:02:51 pm
I use the textmode IDE at version 1.0.12 with FPC 3.2.0, and also have 3.2.3 installed. Windowns 10. I tested your version and KodeZwerg's compact version and always get the expected output, regardless which compiler settings I tried, with 3.2.0 as well as 3.2.3. I would be really surprised if that was an FPC bug. Could you please specify your compiler settings i.e. target processor, optimisation etc.
Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 01:13:39 pm
I use the textmode IDE at version 1.0.12 with FPC 3.2.0, and also have 3.2.3 installed. Windowns 10. I tested your version and KodeZwerg's compact version and always get the expected output, regardless which compiler settings I tried, with 3.2.0 as well as 3.2.3. I would be really surprised if that was an FPC bug. Could you please specify your compiler settings i.e. target processor, optimisation etc.

Thank you the attention to my problem. The target processor is Win32 for i386. The compiler mode is set to Free Pascal dialect. I will be happy to give more information as needed provided you inform where to look at. Thanks again!
Title: Re: Variables as indexes of arrays
Post by: Nitorami on March 31, 2023, 01:26:18 pm
I mean all the settings you find under Options/Compiler. Maybe just make screenprints of tabs Syntax, Generated Code and Processor. Or post your fp.cfg file.
Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 03:23:08 pm
Or post your fp.cfg file.

Here is the content of the fp.cfg: Thanks for your attention.
# Automatically created file, don't edit.
#IFDEF NORMAL
 -TWin32
 -Mfpc
 -Sg
 -Ci
 -O1
 -Cp80386
 -Op80386
 -Rdefault
 -FuC:\FPC\3.2.2\units\$fpctarget
 -FuC:\FPC\3.2.2\units\$fpctarget\*
 -FuC:\FPC\3.2.2\units\$fpctarget\rtl
 -XS
 -g-
 -p-
 -b-
#ENDIF

#IFDEF DEBUG
 -TWin32
 -Mfpc
 -Sg
 -Cr
 -Ci
 -Co
 -Cp80386
 -Op80386
 -Rdefault
 -FuC:\FPC\3.2.2\units\$fpctarget
 -FuC:\FPC\3.2.2\units\$fpctarget\*
 -FuC:\FPC\3.2.2\units\$fpctarget\rtl
 -XS
 -g
 -p-
 -b-
#ENDIF

#IFDEF RELEASE
 -TWin32
 -Mfpc
 -Sg
 -O2
 -Cp80386
 -Op80386
 -Rdefault
 -FuC:\FPC\3.2.2\units\$fpctarget
 -FuC:\FPC\3.2.2\units\$fpctarget\*
 -FuC:\FPC\3.2.2\units\$fpctarget\rtl
 -XS
 -g-
 -p-
 -b-
#ENDIF
Title: Re: Variables as indexes of arrays
Post by: Nitorami on March 31, 2023, 03:44:36 pm
Very odd. I cannot reproduce this even with your fp.cfg file, and I'm out of ideas. If nothing else helps, maybe try a clean install of the latest version.
Title: Re: Variables as indexes of arrays
Post by: Thaddy on March 31, 2023, 04:33:51 pm
And what does your fpc.cfg say? Which has always prevalence.
Although I can not see how the compiler settings can be in any way related to your question.
Title: Re: Variables as indexes of arrays
Post by: Nitorami on March 31, 2023, 09:35:44 pm
Alright, I could identify the conditions under which this happens, and reproduce the issue on a different machine with an AMD processor, FPC 3.2.2, optimization target processor set to 80386, and level 1 optimisation ON, but no other optimisations. It also happens with fpc 3.0.4., but not with 3.3.1. I don't believe it has actively been fixed in 3.3.1, this may just be random.

Faulty output is:
1
2
3
1
2
3
0
0
0
0
1200
0

@JMarques: You can easily get around this, in the IDE under Options/Compiler/Processor just select some newer processor type for optimization, and the problem should be gone forever.

In the meantime, I'll file a bug report. This is a severe issue and should not happen which such a simple code.

Title: Re: Variables as indexes of arrays
Post by: JMarques on March 31, 2023, 11:45:17 pm
in the IDE under Options/Compiler/Processor just select some newer processor type for optimization, and the problem should be gone forever.

Many many thanks! I am so glad you identified the problem!
But in Options/Compiler I don't find the Processor, I only see Syntax Switches and Compiler mode. as in the attachment, can you enlighten me? Many thanks
Title: Re: Variables as indexes of arrays
Post by: Fred vS on April 01, 2023, 12:35:15 am
in the IDE under Options/Compiler/Processor just select some newer processor type for optimization, and the problem should be gone forever.

Many many thanks! I am so glad you identified the problem!
But in Options/Compiler I don't find the Processor, I only see Syntax Switches and Compiler mode. as in the attachment, can you enlighten me? Many thanks

Hello.

In your fp.cfg, remove all the lines with:

  -Cp80386
 -Op80386


@Nitorami : Well seen!  ;D
Title: Re: Variables as indexes of arrays
Post by: Nitorami on April 01, 2023, 08:19:33 am
@JMarques: Your menu looks odd, and tabs are missing. You should see Generated Code, Processor, etc. like in the attached screenshot.

I don't know why that is. Windows 11 ? I also note your awful colors which totally obliterate the purpose of code highlighting in the editor. This happened to me a while ago with a new windows installation, and I literally could not use the console anymore. It turned out that Microsoft had messed up by "modernizing" the console colorscheme which in their opinion came from a time when monitors had poorer contrast than now. While it is true that the colorscheme is old, the reasoning is complete nonsense. The old CRT tube monitors were brighter, faster and had better contrast than now, and there is a reason why professional gamers still use them. You can download MS' "color tool" to reset this shit.

But I still don't know what's wrong with your menu.
Title: Re: Variables as indexes of arrays
Post by: JMarques on April 01, 2023, 11:29:37 am
Hello.
In your fp.cfg, remove all the lines with:
 -Cp80386
 -Op80386[/b]

@Nitorami : Well seen!  ;D

Thank you for your attention. I just installed the 3.2.2 32/64 version and the problem was solved!
Title: Re: Variables as indexes of arrays
Post by: JMarques on April 01, 2023, 11:33:57 am
Alright, I could identify the conditions under which this happens, and reproduce the issue on a different machine with an AMD processor, FPC 3.2.2, optimization target processor set to 80386, and level 1 optimisation ON, but no other optimisations. It also happens with fpc 3.0.4., but not with 3.3.1. I don't believe it has actively been fixed in 3.3.1, this may just be random.
@JMarques: You can easily get around this, in the IDE under Options/Compiler/Processor just select some newer processor type for optimization, and the problem should be gone forever.
In the meantime, I'll file a bug report. This is a severe issue and should not happen which such a simple code.

Nitorani, I just installed the version you have, 3.2.2 32/64 and the problem was solved!!
Thank you very very much for your help! Reproducing the problem was crucial to determine that it wasn't a failure in my hardware.
Title: Re: Variables as indexes of arrays
Post by: JMarques on April 01, 2023, 11:45:24 am
@JMarques: Your menu looks odd, and tabs are missing. You should see Generated Code, Processor, etc. like in the attached screenshot.

Nitorami, I figured out the issue. The menu is there but it just doesn't show, the whole menu line is truncated, but I found that I can click on the line and get the corresponding options. Must be a problem with the size or lay out of the box. But it is functional.

I realized that the selected options are still the same as I had in the old version, but even then the program works correctly, so I don't know if I should choose another processor and optimization options, what do you think?

Thank you again!
Title: Re: Variables as indexes of arrays
Post by: JMarques on April 01, 2023, 12:02:49 pm
In the meantime, I'll file a bug report. This is a severe issue and should not happen which such a simple code.

When I was doing all sorts of crazy tests to figure out the problem I asked the program to write values of row[0][0] to row[0][8], even though row[0] only has three members. Surprizingly, the compiler didn't complain and just printed the nine values. Then I realized that the values for row[0][3] to row[0][5] were actually the values that I had entered for row[1], and the values for row[0][6] to row[0][8] were the values I had attributed to row[2]. Very strange. After I installed the new version and tried to compile the code it immediately pointed out the error in the number of members of row[0].
Perhaps this description can help to better identify the bug.
Title: Re: Variables as indexes of arrays (Solved)
Post by: Nitorami on April 01, 2023, 09:01:01 pm
I reported it, and Jonas confirmed it is a bug in the peephole optimizer for 80386 processors. I hope a developer will look into that, I can't do it.

And yes, to get reliable results, simply set the optimization target processor to Pentium or some other processor and you'll be fine. Alternatively, turn off O1 optimisation.
TinyPortal © 2005-2018