Recent

Author Topic: Variable initialization  (Read 4340 times)

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Variable initialization
« on: February 26, 2025, 06:17:21 pm »
Have this thing on my mind for a while... does the compiler initializes variables with some defaults ?

I wouldn't need to know all. I just need to know boolean. Is there a guarantee every boolean is initialized with False ?

Thing is, I would like to have defaults on objects, like a sort of an implicit constructor, so if I declare in a procedure, say var mySomething:TSomething, I would know mySomething gets automatically populated with default values on variables without me to explicitly call the constructor.

To me, explicit constructors sound absolutely ridiculous. IF constructor exectuion would be skipped if the object was tampered with, by a call to a method or a variable assignment, then constructors would make sense. You would begin any method with a constructor call to make sure defaults are set. Otherwise, there is no difference between procedure and constructor. Constructors are good on classes, but are nothing but cosmetic on objects. They just look fancy on the listing.

So, given that we don't have default constructors, and having an explicit constructor would rise the same question when writing it, "how do I know the object was intialized ?" inside constructor code, my question falls to something far simpler:

Are booleans always initialized to false ?

This way I can absolutely be sure that if I have an Initialized field in the object, this would automatically start on false, so i could call from all methods a procedure Init or constructor Init that would check on that and set some initial defaults.

So, is this the case, does the program initialize booleans always to false, or there is no initilization and that value is random on start ?

« Last Edit: February 26, 2025, 06:20:57 pm by fxeconomist »

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Variable initialization
« Reply #1 on: February 26, 2025, 06:22:05 pm »
You can initialise a variable when defining it - is this what you mean?

Code: Pascal  [Select][+][-]
  1. var
  2.   myVar: Boolean = False;
  3.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Variable initialization
« Reply #2 on: February 26, 2025, 06:49:01 pm »
I believe that you can rely on globals being zeroed. I believe that you can rely on strings and dynamic arrays being initialised to zero length.

You definitely can't rely on anything else on the stack or heap being set to a known default. That includes the specific case that e.g. a floating point number might be set to a pattern of bits which quite simply does not make sense.

Documentation via https://www.freepascal.org/docs.html, in particular https://www.freepascal.org/docs-html/current/ref/refse24.html#x55-750004.4

"By default, simple variables in Pascal are not initialized after their declaration. Any assumption that they contain 0 or any other default value is erroneous: They can contain rubbish." plus the remark that follows that para.

Note also that a string that is extended is not wiped, and can reemerge inconveniently, so something like a password /must/ be overwritten by spaces etc. of the same length before it goes out of scope.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: Variable initialization
« Reply #3 on: February 26, 2025, 06:53:48 pm »
You can initialise a variable when defining it - is this what you mean?
 
Code: Pascal  [Select][+][-]
  1. var
  2.   myVar: Boolean = False;
  3.  

But that doesn't work.

I can't write:

Code: Pascal  [Select][+][-]
  1. type TSomething=object
  2.        Initialized: boolean = false;
  3.  end;
  4.  

[/code]

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: Variable initialization
« Reply #4 on: February 26, 2025, 06:56:48 pm »
I believe that you can rely on globals being zeroed. I believe that you can rely on strings and dynamic arrays being initialised to zero length.

You definitely can't rely on anything else on the stack or heap being set to a known default. That includes the specific case that e.g. a floating point number might be set to a pattern of bits which quite simply does not make sense.

Documentation via https://www.freepascal.org/docs.html, in particular https://www.freepascal.org/docs-html/current/ref/refse24.html#x55-750004.4

"By default, simple variables in Pascal are not initialized after their declaration. Any assumption that they contain 0 or any other default value is erroneous: They can contain rubbish." plus the remark that follows that para.

Note also that a string that is extended is not wiped, and can reemerge inconveniently, so something like a password /must/ be overwritten by spaces etc. of the same length before it goes out of scope.

MarkMLl

So not even booleans are guaranteed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Variable initialization
« Reply #5 on: February 26, 2025, 07:16:09 pm »
So not even booleans are guaranteed.

I am assuming that that is a statement based on your careful reading of the part of the manual I cited >:-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: Variable initialization
« Reply #6 on: February 26, 2025, 08:04:38 pm »
I believe that you can rely on strings and dynamic arrays being initialised to zero length.

MarkMLl

But, if I am to quote you on this, then I could make an Initiliazed field as a string or dynamic array.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Variable initialization
« Reply #7 on: February 26, 2025, 08:12:43 pm »
But, if I am to quote you on this, then I could make an Initiliazed field as a string or dynamic array.

But only initialised to zero length.

A non-dynamic global array will of course be implicitly zeroed... even if the elements are explicitly declared as non-zero (e.g. days of week defined as 1..7, then an array of days of the week). But globals are frowned upon... shame that they're inescapable when used for (e.g.) Lazarus forms.

There are of course ways to initialise most types of variable at or near their declaration, with the notable exception of classes where you have to initialise fields individually in the constructor.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Variable initialization
« Reply #8 on: February 26, 2025, 08:14:35 pm »
But that doesn't work.

I can't write:

Code: Pascal  [Select][+][-]
  1. type TSomething=object
  2.        Initialized: boolean = false;
  3.  end;
  4.  

[/code]
That is not a 'normal' variable declaration rather a field inside an object/class.
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Variable initialization
« Reply #9 on: February 26, 2025, 08:31:11 pm »
That is not a 'normal' variable declaration rather a field inside an object/class.

With the normal kvetching about the distinction between an object and an instantiated class :-(

Detail at https://github.com/zsoltszakaly/OOPstructuresinpascal

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: Variable initialization
« Reply #10 on: February 26, 2025, 09:22:31 pm »
But, if I am to quote you on this, then I could make an Initiliazed field as a string or dynamic array.

But only initialised to zero length.


Still, it's something. i only needed one variable type to test against. Thus, I should be able to write.

Code: Pascal  [Select][+][-]
  1. type TSomething=object
  2.        Initialized:array of boolean;
  3.        SomeData: integer;
  4.       procedure TrueConstructor;
  5.       end;
  6.  
  7. procedure TSomething.TrueConstructor;
  8. begin
  9. if Length(self.Initialized)=false
  10.    then begin
  11.       self.SomeData:=3;
  12.      SetLength(self.Initialized,1);
  13.    end;
  14. end;
  15.  

This way every method can first call TrueConstructor to have some defaults loaded, while every subsequent call will leave the object untouched.

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: Variable initialization
« Reply #11 on: February 26, 2025, 09:26:59 pm »
That is not a 'normal' variable declaration rather a field inside an object/class.

With the normal kvetching about the distinction between an object and an instantiated class :-(

Detail at https://github.com/zsoltszakaly/OOPstructuresinpascal

MarkMLl

I still have to learn about classes and se-ri-a-li-za-ble things.
Back when I was a teenager in the times of BP7, classes were indistinct from objects. I think the "class" keyword did not exist. Object type declaration was the equivalent of C++ class
and object variable declaration was called "instance", and that was the equivalent of the c++ object. But I don't know more about C++ than general stuff.

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: Variable initialization
« Reply #12 on: February 26, 2025, 09:32:00 pm »
Hi
PLEASE read the article @MarkMLl linked, your thinking is wrong.
You need to study, before you start crawling...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Variable initialization
« Reply #13 on: February 26, 2025, 09:52:22 pm »
PLEASE read the article @MarkMLl linked, your thinking is wrong.
You need to study, before you start crawling...
+1 because it is actually a decent write-up especially for a crash-course..
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Variable initialization
« Reply #14 on: February 26, 2025, 10:25:28 pm »
Back when I was a teenager in the times of BP7,

When I was a teenager, logic was made out of DTL. When I got my first job after graduating, most of the machines still used Fairchild CTL since TTL was "NIH".

I've tried my best to be careful with my terminology throughout this thread, but I suggest that you /really/ need to read the document I cited, as well as the standard manuals.

Having said which, please don't expect me to defend the way that Object Pascal provides multiple ways to do the same sort of thing: conventional wisdom has it that a language is improved by generalising, not by tacking on more (tacky) syntax.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018