Forum > Beginners
Strange behaviour of BOOLEAN type
vladbfg:
Hi all.
I use compiler/IDE: Lazarus 3.2 FPC 3.2.2 x86_64-win64-win32/win64
There is a strange behavoir of boolean calculation, when boolean variable is part of array/structure
Could anybody explain the results of code below:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type Tstruc2 = record a: boolean;end; type Tstruc = record i: integer; ar: array[1..1] of Tstruc2;end; var mas : array[1..1] of Tstruc; procedure TFMain.Button3Click(Sender: TObject); var i, j, k:integer;begin k:=1; for i:=1 to 1 do for j:=1 to 1 do begin mas[i].ar[j].a := k=1; end; Fmain.MemoLog.Lines.Add( BoolToStr( mas[1].ar[1].a, true ) );end;
Then I see "False" on the screen. Debugger shows value False too.
But when I use another var, then code works as expected
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- b:= k=1; mas[i].ar[j].a :=b;
Then there is TRUE in the mas[1].ar[1].a
Could you tests this simple code?
bytebites:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; uses sysutils; type Tstruc2 = record a: boolean;end; type Tstruc = record i: integer; ar: array[1..1] of Tstruc2;end; var mas : array[1..1] of Tstruc; i, j, k:integer;begin k:=1; for i:=1 to 1 do for j:=1 to 1 do begin mas[i].ar[j].a := k=1; end; writeln( BoolToStr( mas[1].ar[1].a, true ) );end.
Output is True, Fpc version trunk.
alpine:
I can confirm that:
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---/home/alpi/fpcupdeluxe_v2_2_0m/fpcupdeluxe/projects/project1.lpr:26 mas[i].ar[j].a := k=1;00000000004010C4 8B45FC mov eax,[rbp-$04]00000000004010C7 8B4DF8 mov ecx,[rbp-$08]00000000004010CA 48C1E003 shl rax,$0300000000004010CE 837DF401 cmp dword ptr [rbp-$0C],$0100000000004010D2 488D15D7F70700 lea rdx,[rip+$0007F7D7]00000000004010D9 4801D0 add rax,rdx00000000004010DC 0F944408FB setz byte ptr [rcx+rax-$05] The additional add changes the Z. FPC 3.2.2 for 64-bit target.
32-bit is OK.
Thaddy:
Yes. And I WARNED >:D for that... earlier.... I hope that gets reversed. Bad idea.
The issue was mentioned before, it is the expansion that causes the trouble when using higher optimization levels. (It is fine in -O1 or -O- state)
This can easily be avoided by excuding Boolean and Bool types of being expanded.
vladbfg:
--- Quote from: alpine on May 30, 2024, 08:51:05 am ---I can confirm that:
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---/home/alpi/fpcupdeluxe_v2_2_0m/fpcupdeluxe/projects/project1.lpr:26 mas[i].ar[j].a := k=1;00000000004010C4 8B45FC mov eax,[rbp-$04]00000000004010C7 8B4DF8 mov ecx,[rbp-$08]00000000004010CA 48C1E003 shl rax,$0300000000004010CE 837DF401 cmp dword ptr [rbp-$0C],$0100000000004010D2 488D15D7F70700 lea rdx,[rip+$0007F7D7]00000000004010D9 4801D0 add rax,rdx00000000004010DC 0F944408FB setz byte ptr [rcx+rax-$05] The additional add changes the Z. FPC 3.2.2 for 64-bit target.
32-bit is OK.
--- End quote ---
I don't know asm, this is my debug window. Are there all ok?
Navigation
[0] Message Index
[#] Next page