Forum > FPC development

[SOLVED] Improvement of function SetCompileModeSwitch

(1/1)

lagprogramming:
compiler/scanner.pas has function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
Within the code of the function "s:=copy(s,1,length(s)-1);" can be encountered twice. The attached patch replaces it with "setlength(s,length(s)-1);".

AlexTP:
Reported to https://gitlab.com/freepascal.org/fpc/source/-/issues/40525

vangli:
Hi. Just of curriosity. Isn't it?


--- 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";}};} ---s:=copy(s,1,length(s)-1);
will guaranty that "s" is minimum an empty string if it happens that "s" is single byte or already an empty string.


--- 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";}};} ---setlength(s,length(s)-1);
will release memory if "s" is one byte long or already an empty string. Then the rest of the function SetCompileModeSwitch will crash?

TRon:

--- Quote from: vangli on November 19, 2023, 09:12:35 am ---will release memory if "s" is one byte long or already an empty string. Then the rest of the function SetCompileModeSwitch will crash?

--- End quote ---
Nah, not really  :)


--- 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";}};} ---case s[length(s)] of 

vangli:
Thks TRon. I may have been (mis)leaded by https://www.freepascal.org/docs-html/rtl/system/setlength.html where it states the following:

"Note that SetLength is governed by the Copy-On-Write principle for strings and dynamic arrays: the reference count after a call to SetLength will be 1 (except when the length is zero, then the array is removed from memory). "

I read the parenthesis to include both strings and arrays, but it may only be true for array length.

Navigation

[0] Message Index

Go to full version