Recent

Author Topic: Quickly set unit variables to their default value  (Read 2168 times)

dadycool

  • New member
  • *
  • Posts: 8
Quickly set unit variables to their default value
« on: January 12, 2022, 06:54:15 pm »
Hi all

I have a unit(a)

This unit(a) contain lots of variables, and it is referenced in my other units as a placeholder.

Question is how I can quickly reset variables in unit(a) to it's default value?

Thanks a lot!

winni

  • Hero Member
  • *****
  • Posts: 3045
Re: Quickly set unit variables to their default value
« Reply #1 on: January 12, 2022, 10:59:42 pm »
Hi!

But if you want to reset all your values to zero you can use a record:

Code: Pascal  [Select][+][-]
  1. var myrec = record
  2.              a,b,c : integer;
  3.               F1,F2 : single;
  4.               ...
  5.              end; // record
  6.  
  7. ...
  8. begin
  9. Fillchar (myrec, sizeof(myrec),0)
  10. end;
  11.  
  12.  
Winni



Kays

  • Sr. Member
  • ****
  • Posts: 423
  • Whasup!?
    • KaiBurghardt.de
Re: Quickly set unit variables to their default value
« Reply #2 on: January 12, 2022, 11:30:48 pm »
[…] Question is how I can quickly reset variables in unit(a) to it's default value? […]
In Pascal, variables don’t have a “default value”. The programming language specification does not say a Boolean variable has e.g. true as a “default”. I think, Jamie’s approach of writing and calling a procedure setting your variables is probably the way you wanna go. (NB: A resourceString does have a default/fallback value, but you probably aren’t asking for such.)

[…]
Code: Pascal  [Select][+][-]
  1. []
  2. Fillchar (myrec, sizeof(myrec),0)
  3. []
Please use default. I’m not even sure if it will properly treat a record containing for instance an ANSIString (i.e. decrement reference count), but it’s definitely better than a blunt “erase everything with #0!”.
Yours Sincerely
Kai Burghardt

speter

  • Sr. Member
  • ****
  • Posts: 271
Re: Quickly set unit variables to their default value
« Reply #3 on: January 13, 2022, 12:41:48 am »
Combining what Jamie & Kays wrote, you can try something like:

Code: Pascal  [Select][+][-]
  1. unit decU;
  2. // global variable declarations
  3.  
  4. {$mode ObjFPC}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.  
  11. const
  12.   sz=100;
  13. type
  14.   tmyrec = record
  15.     some : integer;
  16.     stuff : double;
  17.     here : string;
  18.   end;
  19.   tmyarray = array [1..sz] of tmyrec;
  20.   tdynamic = array of tmyrec;
  21. var
  22.   anint : integer = 0;
  23.   adouble : double  = 0.0;
  24.   another : integer;
  25.  
  26.   anarray : tmyarray;
  27.   adynamic : tdynamic;
  28. const
  29.   azero : tmyrec = (some:0; stuff:0.0; here:'');
  30.  
  31. procedure InitDec;
  32.  
  33. implementation
  34.  
  35. procedure InitDec;
  36. var
  37.   a : byte;
  38. begin
  39.   // don't need to initialise "anint" or "adouble"
  40.   another := 0;
  41.  
  42.   anarray := default(tmyarray);
  43.  
  44.   // this compiles, but I'm not sure what it does...
  45.   //adynamic := default(tdynamic);
  46.  
  47.   setlength(adynamic,sz);
  48.   for a := 0 to sz-1 do
  49.     adynamic[a] := azero;
  50. end;
  51.  
  52. begin
  53.   InitDec;
  54. end.

This allows you to call InitDec multiple times (if appropriate) or simply let the unit initialisation do its job. :)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.2.0 / FPC 3.2.2 / Windows 11 (64bit)

dseligo

  • Hero Member
  • *****
  • Posts: 580
Re: Quickly set unit variables to their default value
« Reply #4 on: January 13, 2022, 12:54:25 am »
  // don't need to initialise "anint" or "adouble"
This allows you to call InitDec multiple times (if appropriate) or simply let the unit initialisation do its job. :)

If he won't initialize anint and adouble in code then there is no initialization when InitDec is called multiple times.
And if there is no need for multiple initialization, you don't have to initialize variables to 0: global variables are initialized by FPC.

speter

  • Sr. Member
  • ****
  • Posts: 271
Re: Quickly set unit variables to their default value
« Reply #5 on: January 13, 2022, 01:05:26 am »
If he won't initialize anint and adouble in code then there is no initialization when InitDec is called multiple times.
And if there is no need for multiple initialization, you don't have to initialize variables to 0: global variables are initialized by FPC.
All true. :) You are correct, I should have included initialiation for anint and adouble.

cheers
S.

PS: Can anyone comment on whether "defaulting" a dynamic array (that hasn't had its length set) does anything useful?
Code: Pascal  [Select][+][-]
  1. adynamic := default(tdynamic);
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.2.0 / FPC 3.2.2 / Windows 11 (64bit)

dadycool

  • New member
  • *
  • Posts: 8
Re: Quickly set unit variables to their default value
« Reply #6 on: January 13, 2022, 01:22:15 am »
I like special function as jamie and speter suggest but using record with default as per winni and Kays seems easier to maintain. I will evaluate both and see how it goes.

p/s: Wish this forum had a HUG button so I can HUG everyone with their excellent answers. All of your comments make sense and shed me light  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Quickly set unit variables to their default value
« Reply #7 on: January 13, 2022, 07:42:37 am »
@winni, a.o.:
FYI
AFAIK An important difference between Default() and fillChar etc is that Default() is an intrinsic, That means the compiler can generate less code or sometimes no code at all! :o
It will be resolved by the compiler at compile time and less or in some cases no code is executed by the program,
The fillchar is in effect delegated to the compiler.
So always use default() if it is available for the type. (I believe only file types are excluded)

This is documented for Delphi - hard to find, but it is - and I expect this is indeed the same in FPC and it looks like it is: compile with -al to see the difference in generated assembler. Default() generates considerably less code than FillChar.
See also wikipedia on intrinsics
« Last Edit: January 13, 2022, 10:42:51 am by Thaddy »
Путин преступник. Россияне дезинформированы.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4014
  • Compiler Developer
Re: Quickly set unit variables to their default value
« Reply #8 on: January 13, 2022, 10:07:06 am »
[…]
Code: Pascal  [Select][+][-]
  1. []
  2. Fillchar (myrec, sizeof(myrec),0)
  3. []
Please use default. I’m not even sure if it will properly treat a record containing for instance an ANSIString (i.e. decrement reference count), but it’s definitely better than a blunt “erase everything with #0!”.

One needs to use Finalize(myrec) before doing the FillChar so that this works correctly. But yes, I agree that Default is the way to go.

 

TinyPortal © 2005-2018