Recent

Author Topic: Initializing large constant  (Read 2883 times)

440bx

  • Hero Member
  • *****
  • Posts: 6096
Initializing large constant
« on: September 29, 2024, 11:41:31 pm »
Hello,

I'd like to initialize an array constant with spaces, something along the lines of:
Code: Pascal  [Select][+][-]
  1. const
  2.   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.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1664
Re: Initializing large constant
« Reply #1 on: September 30, 2024, 12:21:20 am »
Sure, just copy and paste :)

Code: Text  [Select][+][-]
  1. const
  2.   SPACE_ARRAY: array[0..8191] of char =
  3.     '                                                                                                                                                                                                                                                                ' +
  4.     '                                                                                                                                                                                                                                                                ' +
  5.     '                                                                                                                                                                                                                                                                ' +
  6.     '                                                                                                                                                                                                                                                                ' +
  7.     '                                                                                                                                                                                                                                                                ' +
  8.     '                                                                                                                                                                                                                                                                ' +
  9.     '                                                                                                                                                                                                                                                                ' +
  10.     '                                                                                                                                                                                                                                                                ' +
  11.     '                                                                                                                                                                                                                                                                ' +
  12.     '                                                                                                                                                                                                                                                                ' +
  13.     '                                                                                                                                                                                                                                                                ' +
  14.     '                                                                                                                                                                                                                                                                ' +
  15.     '                                                                                                                                                                                                                                                                ' +
  16.     '                                                                                                                                                                                                                                                                ' +
  17.     '                                                                                                                                                                                                                                                                ' +
  18.     '                                                                                                                                                                                                                                                                ' +
  19.     '                                                                                                                                                                                                                                                                ' +
  20.     '                                                                                                                                                                                                                                                                ' +
  21.     '                                                                                                                                                                                                                                                                ' +
  22.     '                                                                                                                                                                                                                                                                ' +
  23.     '                                                                                                                                                                                                                                                                ' +
  24.     '                                                                                                                                                                                                                                                                ' +
  25.     '                                                                                                                                                                                                                                                                ' +
  26.     '                                                                                                                                                                                                                                                                ' +
  27.     '                                                                                                                                                                                                                                                                ' +
  28.     '                                                                                                                                                                                                                                                                ' +
  29.     '                                                                                                                                                                                                                                                                ' +
  30.     '                                                                                                                                                                                                                                                                ' +
  31.     '                                                                                                                                                                                                                                                                ' +
  32.     '                                                                                                                                                                                                                                                                ' +
  33.     '                                                                                                                                                                                                                                                                ' +
  34.     '                                                                                                                                                                                                                                                                ';

I would rather use something like this:
Code: Pascal  [Select][+][-]
  1. FillChar(SPACE_ARRAY, Length(SPACE_ARRAY), ' ');

jamie

  • Hero Member
  • *****
  • Posts: 7543
Re: Initializing large constant
« Reply #2 on: September 30, 2024, 12:33:17 am »
Hello,

I'd like to initialize an array constant with spaces, something along the lines of:
Code: Pascal  [Select][+][-]
  1. const
  2.   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.
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 6096
Re: Initializing large constant
« Reply #3 on: September 30, 2024, 12:54:18 am »
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.)

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Joanna

  • Hero Member
  • *****
  • Posts: 1418
Re: Initializing large constant
« Reply #4 on: September 30, 2024, 02:26:41 am »
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][+][-]
  1. Var x:integer;
  2.       S:string;
  3. Begin
  4. s:= ‘ar_blanks: array[0..8191] of char = (;
  5. For x:= 0 to 8191 do
  6.       If x <> 8191
  7.           Then s:= s+’’ ’,
  8.            Else s:=s+ ’’ ’;);
  9. End;
  10.  

      S:l [/code]

440bx

  • Hero Member
  • *****
  • Posts: 6096
Re: Initializing large constant
« Reply #5 on: September 30, 2024, 07:00:59 am »
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.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Initializing large constant
« Reply #6 on: September 30, 2024, 07:58:10 am »
Machine generate a file with lots of spaces and include it:
Code: Pascal  [Select][+][-]
  1. program testaa;
  2. {$mode objfpc}{$J+}
  3. (* generator
  4. program genspaces;
  5. var
  6.  f:Text;
  7.  i:integer;
  8. begin
  9.  Assign(f, 'spaces.txt');
  10.  rewrite(f);
  11.  write(f,#39);
  12.  for i := 0 to 8191 do write(f,#32);
  13.  write(f,#39);
  14.  close(f);
  15. end.
  16. *)
  17. const
  18.   testarr:packed array[0..8191] of ansichar = {$I spaces.txt};
  19. begin
  20. end.
     
Probably faster than the other solutions including writing and running the generator.                           
« Last Edit: September 30, 2024, 08:23:07 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Joanna

  • Hero Member
  • *****
  • Posts: 1418
Re: Initializing large constant
« Reply #7 on: September 30, 2024, 09:36:51 am »
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.
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

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Initializing large constant
« Reply #8 on: September 30, 2024, 09:46:37 am »
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.
« Last Edit: September 30, 2024, 09:49:02 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Joanna

  • Hero Member
  • *****
  • Posts: 1418
Re: Initializing large constant
« Reply #9 on: September 30, 2024, 10:01:46 am »
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

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Initializing large constant
« Reply #10 on: September 30, 2024, 10:26:10 am »
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.
« Last Edit: September 30, 2024, 10:35:49 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Joanna

  • Hero Member
  • *****
  • Posts: 1418
Re: Initializing large constant
« Reply #11 on: September 30, 2024, 10:33:46 am »
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’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

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Initializing large constant
« Reply #12 on: September 30, 2024, 10:39:28 am »
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.)
« Last Edit: September 30, 2024, 10:44:14 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Joanna

  • Hero Member
  • *****
  • Posts: 1418
Re: Initializing large constant
« Reply #13 on: September 30, 2024, 11:12:31 am »
Indeed . That’s probably 440bx’s objective.

Zvoni

  • Hero Member
  • *****
  • Posts: 3270
Re: Initializing large constant
« Reply #14 on: September 30, 2024, 11:14:47 am »
Fooling around

Unit1
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode ObjFPC}{$H+}
  3. {$modeswitch advancedrecords}
  4. interface
  5. uses Classes, SysUtils;
  6. Type
  7.   TMyChar = packed record
  8.     MyChar:Char;
  9.     class operator Initialize(var aRec:TMyChar);
  10.   end;
  11.   PMyChar=^TMyChar;
  12.  
  13. Var MyChar:Array[0..9] Of TMyChar;
  14.  
  15. implementation
  16. Var i:Integer;
  17.     P:PMyChar;
  18.  
  19. class operator TMyChar.Initialize(var aRec: TMyChar);
  20. begin
  21.   aRec.MyChar:='x';  //Replace with space
  22.   //Writeln('Create');
  23. end;
  24. initialization
  25.   For i:=Low(MyChar) To high(MyChar) Do
  26.     Begin
  27.       New(p);
  28.       MyChar[i]:=P^;
  29.       Dispose(p);
  30.     End;
  31. finalization
  32. end.

Usage
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. uses unit1;
  4. Var i:Integer;
  5.     a:Array[0..9] Of Char absolute MyChar;
  6. begin
  7.   For i:=Low(a) To High(a) Do Writeln(a[i]);
  8. end.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018