Initializing large constant lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2024-09-29 21:41 Hello, I'd like to initialize an array constant with spaces, something along the lines of: 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 const  SPACE_ARRAY = array[0..8191] of char = ' ';but that only initializes the first element of the array to a space.  The rest of the elements are nulls.  Is there a way to tell FPC to initialize the _entire_ array to spaces ?  (I don't even want a null terminator but, if there must be one, so be it.) Thank you for your help. ---------------------------------------------------------------------- dseligo · 2024-09-29 22:21 Sure, just copy and paste Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  SPACE_ARRAY: array[0..8191] of char =    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +  '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                        ' +    '                                                                                                                                                                                                                                                          ' +    '                                                                                                                                                                                                                                                        '; I would rather use something like this: 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 FillChar(SPACE_ARRAY, Length(SPACE_ARRAY), ' '); ---------------------------------------------------------------------- jamie · 2024-09-29 22:33 Quote from: 440bx on September 29, 2024, 11:41:31 pmHello, I'd like to initialize an array constant with spaces, something along the lines of: 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 const  SPACE_ARRAY = array[0..8191] of char = ' ';but that only initializes the first element of the array to a space.  The rest of the elements are nulls.  Is there a way to tell FPC to initialize the _entire_ array to spaces ?  (I don't even want a null terminator but, if there must be one, so be it.) Thank you for your help. You have two pages of memory you want to wipe at startup, I think the best place to do that is in the initialization section of the unit using the FillCar procedure as stated by the other poster. ---------------------------------------------------------------------- 440bx · 2024-09-29 22:54 Thank you dseligo and jamie. I was hoping for a solution that didn't require code and would adapt automatically to the size of the array (which could change in some situations.) ---------------------------------------------------------------------- Joanna · 2024-09-30 00:26 I have an idea someone gave me for populating rather large arrays. You will need to make a program to put the text of the declaration into a file or string variable you can copy then copy paste that into your program something like this 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 x:integer;      S:string;Begins:= ‘ar_blanks: array[0..8191] of char = (‘;For x:= 0 to 8191 do      If x <> 8191          Then s:= s+’’ ’,’         Else s:=s+ ’’ ’;’);End;        S:l [/code] ---------------------------------------------------------------------- 440bx · 2024-09-30 05:00 Thank you Joanna. I would really like some way to have the compiler initialize the structure instead of having code do it but, maybe code is the only way to do it. ---------------------------------------------------------------------- Thaddy · 2024-09-30 05:58 Machine generate a file with lots of spaces and include it: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 program testaa;{$mode objfpc}{$J+}(* generatorprogram genspaces;var f:Text; i:integer;begin Assign(f, 'spaces.txt'); rewrite(f); write(f,#39); for i := 0 to 8191 do write(f,#32); write(f,#39); close(f);end.*)const  testarr:packed array[0..8191] of ansichar = {$I spaces.txt};beginend.    Probably faster than the other solutions including writing and running the generator. ---------------------------------------------------------------------- Joanna · 2024-09-30 07:36 Quote from: 440bx on September 30, 2024, 07:00:59 amThank you Joanna. I would really like some way to have the compiler initialize the structure instead of having code do it but, maybe code is the only way to do it. You’re welcome  It works for me, I needed to store a large number of tcolor constants in an array. I think your code will be much easier to implement. ---------------------------------------------------------------------- Thaddy · 2024-09-30 07:46 I would always prefer a simple code generator and an include, because that fits better to what 440bx is trying to achieve. The code generator needs only be run once and the const is initialized at declaration time. It is a technique I often used in the past in precisely such cases. To the compiler it is the same as a much shorter array that you would initialize yourself. ---------------------------------------------------------------------- Joanna · 2024-09-30 08:01 Using a loop to initialize the code would certainly take up less space in the source code but maybe 440bx wants his program to work faster by having it ready at runtime. I have an entire Large  table That could in theory be generated at runtime but I prefer to do all the calculations just once and save results in a database. ---------------------------------------------------------------------- Thaddy · 2024-09-30 08:26 Yes, but in your example you could have used StringOfChar from system. Alas that would not work with static vars a.k.a. writeable constants. My solution to avoid typing works for all consts. Even large consts need to be initialized at compile time. (you could also use stringofchar in my generator instead of the explicit loop) So also in your case a simple code generator would be beneficial. ---------------------------------------------------------------------- Joanna · 2024-09-30 08:33 Quote from: Thaddy on September 30, 2024, 10:26:10 amYes, but in your example you could have used StringOfChar from system. Alas that would not work with static vars a.k.a. writeable constants. My solution to avoid typing works for all consts. Even large consts need to be initialized at compile time. You’re right, I forgot that these constant arrays can have their values changed at runtime, so they are not the same as  other types of constants. But still I would hope that a static constant would take less resources than generating values at runtime. I have no idea about how the inner workings of the compiler stores them in the exe file. ---------------------------------------------------------------------- Thaddy · 2024-09-30 08:39 Depending on the J+/J- state in a read only data section(.rodata) or a .data section, but this can be platform dependent as PascalDragon pointed out last week. This will indeed increase the size of your executable over using and initializing a var, but the results are potentially much faster. (e.g. look-up tables with precomputed values.) ---------------------------------------------------------------------- Joanna · 2024-09-30 09:12 Indeed . That’s probably 440bx’s objective. ---------------------------------------------------------------------- Zvoni · 2024-09-30 09:14 Fooling around Unit1 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 unit Unit1;{$mode ObjFPC}{$H+}{$modeswitch advancedrecords}interfaceuses Classes, SysUtils;Type  TMyChar = packed record    MyChar:Char;    class operator Initialize(var aRec:TMyChar);  end;  PMyChar=^TMyChar; Var MyChar:Array[0..9] Of TMyChar; implementationVar i:Integer;    P:PMyChar; class operator TMyChar.Initialize(var aRec: TMyChar);begin  aRec.MyChar:='x';  //Replace with space  //Writeln('Create');end;initialization  For i:=Low(MyChar) To high(MyChar) Do    Begin      New(p);      MyChar[i]:=P^;      Dispose(p);    End;finalizationend. Usage 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 program Project1;{$mode objfpc}{$H+}uses unit1;Var i:Integer;    a:Array[0..9] Of Char absolute MyChar;begin  For i:=Low(a) To High(a) Do Writeln(a[i]);end. ---------------------------------------------------------------------- Thaddy · 2024-09-30 09:24 Zvoni, illigal use of var... ---------------------------------------------------------------------- Zvoni · 2024-09-30 09:34 Quote from: Thaddy on September 30, 2024, 11:24:55 amZvoni, illigal use of var...  Huh? Works for me with FPC3.2.2-32Bit - Laz2.2.2-32Bit (Sorry, should have mentioned it?) ---------------------------------------------------------------------- Thaddy · 2024-09-30 09:35 What does the subject say?  My solution sticks to it... ---------------------------------------------------------------------- Zvoni · 2024-09-30 09:42 Quote from: Thaddy on September 30, 2024, 11:35:37 amWhat does the subject say?  My solution sticks to it... Arrgghh..... Mea Culpa....missed that it's about a CONSTANT... Yeah, your solution works, since it gets resolved during compile-time, not runtime ---------------------------------------------------------------------- PascalDragon · 2024-10-01 21:01 Quote from: 440bx on September 29, 2024, 11:41:31 pmThe rest of the elements are nulls. Please note that a array of Char can be considered a special case, because the compiler will essentially initialize it like a NUL- terminated string. For other array types this wouldn't even work, there you need to really provide an array declaration. If we would have more ISO Extended Pascal support already then there'd be a solution for you: 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  TMyArr = array[0..8191] of Char;const  SPACE_ARRAY = TMyArr[otherwise ' ']; ---------------------------------------------------------------------- 440bx · 2024-10-01 21:55 Quote from: PascalDragon on October 01, 2024, 11:01:53 pmIf we would have more ISO Extended Pascal support already then there'd be a solution for you: 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  TMyArr = array[0..8191] of Char;const  SPACE_ARRAY = TMyArr[otherwise ' '];That would be very nice Thank you for making it clear that, at least at this time, there is no simple/straightforward way to have the compiler initialize the constant as desired. ---------------------------------------------------------------------- Thaddy · 2024-10-02 14:31 I have never seen that in the wild... stick to my simple generator. ---------------------------------------------------------------------- PascalDragon · 2024-10-03 11:37 Quote from: Thaddy on October 02, 2024, 04:31:03 pmI have never seen that in the wild... Of course you haven't. There aren't that many compilers supporting ISO Extended Pascal after all. ---------------------------------------------------------------------- Thaddy · 2024-10-03 13:04 Well, I will ask Scott Franco. I understood - maybe from him - it was implemented in P5x. In that case I probably already have it, unless it did not make it to the Pascal to  GNU C transpiler. (P5C). ---------------------------------------------------------------------- marcov · 2024-10-03 15:13 Quote from: Thaddy on October 03, 2024, 03:04:13 pmWell, I will ask Scott Franco. I understood - maybe from him - it was implemented in P5x. In that case I probably already have it, unless it did not make it to the Pascal to  GNU C transpiler. (P5C). Afaik p5 was mostly about ISO 7185, not extended pascal. p5c seems to also only list 7185 level 1 as status: https://sourceforge.net/p/pascal-p5c/wiki/Home/ Prospero and GPC are the extended pascal's gotos afaik. ---------------------------------------------------------------------- Thaddy · 2024-10-03 15:25 Marco it is P5x as a literal where the x means x, not a substitution char, but indicates extended pascal.