* * *

Author Topic: Class (static) fields of class. Initial values.  (Read 1162 times)

FTurtle

  • Full Member
  • ***
  • Posts: 209
Class (static) fields of class. Initial values.
« on: December 28, 2017, 07:57:01 pm »
 
Code: Pascal  [Select]
  1.  TSomeClass = class
  2.   private class var
  3.     a: Pointer;
  4.   public
  5.     b: Pointer;
  6.   end;
  7.  

Situation for regular fields (b) is known.
I interesting on class (static) fields (a).
In fact after program is run, TSomeClass.a=nil
But I can't find it in FPC documentation.
So, the question:
Can I rely on the fact that after starting program TSomeClass.a=nil

molly

  • Hero Member
  • *****
  • Posts: 2214
Re: Class (static) fields of class. Initial values.
« Reply #1 on: December 28, 2017, 08:13:14 pm »
According to class variables documentation:
Quote
Similar to objects, a class can contain static fields or class variables: these fields or variables are global to the class, and act like global variables, but are known only as part of the class. They can be referenced from within the classes’ methods, but can also be referenced from outside the class by providing the fully qualified name.
Variable documentation states:
Quote
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. To remedy this, the concept of initialized variables exists. The difference with normal variables is that their declaration includes an initial value, as can be seen in the diagram in the previous section.
and
Quote
Managed types are an exception to this rule: Managed types are always initialized: in general this means setting the reference count to zero, or setting the pointer value of the type to Nil. See section 3.9, page 215
Answer from documentation: it depends.

FTurtle

  • Full Member
  • ***
  • Posts: 209
Re: Class (static) fields of class. Initial values.
« Reply #2 on: December 28, 2017, 09:07:44 pm »
I read what you have quoted. It was the cause of question.
I'm cautious about phrases like "acts like" and their extrapolation.
I prefer explicit mentions.
I interesting simple types, so the answer is still "No".
But may be there is something what I don't know.
« Last Edit: December 28, 2017, 09:11:15 pm by FTurtle »

taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: Class (static) fields of class. Initial values.
« Reply #3 on: December 28, 2017, 09:16:30 pm »
Correct. Even if the behavior is documented it is possible that the next version of the compiler will alter the initialization. It is far better to make sure that your static variables are initialized in what ever value you desire by setting it on the initialization section of the unit they are defined. I'm assuming that you do try to keep the static fields to a minimum.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 5201
Re: Class (static) fields of class. Initial values.
« Reply #4 on: December 28, 2017, 09:17:20 pm »
Well let me spell it out:  So yes, the class var is nil. And that is as per the documentation.
« Last Edit: December 28, 2017, 09:19:10 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

FTurtle

  • Full Member
  • ***
  • Posts: 209
Re: Class (static) fields of class. Initial values.
« Reply #5 on: December 28, 2017, 10:22:20 pm »
I'm assuming that you do try to keep the static fields to a minimum.

The goal is to hide global typed constant used in single method.
First I wished make it as static field but finally I just moved  it inside method.
This solution even is better because we immediately see that identifier is used nowhere else.

Well let me spell it out:  So yes, the class var is nil. And that is as per the documentation.

It would be good if you could provide a link.

PascalDragon

  • Jr. Member
  • **
  • Posts: 58
  • Compiler Developer
Re: Class (static) fields of class. Initial values.
« Reply #6 on: January 02, 2018, 10:47:22 pm »
Well let me spell it out:  So yes, the class var is nil. And that is as per the documentation.

Yes and no. Yes: As of now for all targets FPC supports global variables (and static variables) are initialized to 0, but that is not guaranteed by the compiler, but by the OS, so if the OS decides not to do it then it won't be! That is also why we do not document this (thus the No).

Thaddy

  • Hero Member
  • *****
  • Posts: 5201
Re: Class (static) fields of class. Initial values.
« Reply #7 on: January 02, 2018, 11:23:35 pm »
Well let me spell it out:  So yes, the class var is nil. And that is as per the documentation.

Yes and no. Yes: As of now for all targets FPC supports global variables (and static variables) are initialized to 0, but that is not guaranteed by the compiler, but by the OS, so if the OS decides not to do it then it won't be! That is also why we do not document this (thus the No).

UUhhh.... In trunk.... See... http://wiki.freepascal.org/management_operators ...  :P
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

hnb

  • Full Member
  • ***
  • Posts: 208
Re: Class (static) fields of class. Initial values.
« Reply #8 on: January 03, 2018, 09:47:49 am »
UUhhh.... In trunk.... See... http://wiki.freepascal.org/management_operators ...  :P
Finally we have some documentation for management operators.  :o  thanks.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Thaddy

  • Hero Member
  • *****
  • Posts: 5201
Re: Class (static) fields of class. Initial values.
« Reply #9 on: January 03, 2018, 11:52:47 am »
UUhhh.... In trunk.... See... http://wiki.freepascal.org/management_operators ...  :P
Finally we have some documentation for management operators.  :o  thanks.
Now get that default in, Maciej (and Sven..). Happy New Year.

Feel free to edit: I simply made some small examples and used the notes from the feature announcement.
I still need to add some more examples, I guess. (like a powerful debugging tool in just a few lines...)

Regards,

Thaddy
« Last Edit: January 03, 2018, 12:09:42 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

PascalDragon

  • Jr. Member
  • **
  • Posts: 58
  • Compiler Developer
Re: Class (static) fields of class. Initial values.
« Reply #10 on: January 05, 2018, 04:02:35 pm »
Well let me spell it out:  So yes, the class var is nil. And that is as per the documentation.

Yes and no. Yes: As of now for all targets FPC supports global variables (and static variables) are initialized to 0, but that is not guaranteed by the compiler, but by the OS, so if the OS decides not to do it then it won't be! That is also why we do not document this (thus the No).

UUhhh.... In trunk.... See... http://wiki.freepascal.org/management_operators ...  :P

Records with management operators have explicit initializers and finalizers that are called by the RTL just like for any other managed types. This does in no way change how variables of primitive types or records without management operators behave. In fact the initial value of a managed type (String, interface, record with management operators) can be considered as undefined before the initialization code is run and that does include global variables.
So this changes nothing at all regarding what I said. In addition to that the official, sanctioned documentation is at http://www.freepascal.org/docs.var, so even if anyone would add a wiki entry with "globals are always initialized to 0" that would in no way be considered as binding.

ASerge

  • Hero Member
  • *****
  • Posts: 536
Re: Class (static) fields of class. Initial values.
« Reply #11 on: January 05, 2018, 06:37:15 pm »
Yes and no. Yes: As of now for all targets FPC supports global variables (and static variables) are initialized to 0, but that is not guaranteed by the compiler, but by the OS, so if the OS decides not to do it then it won't be! That is also why we do not document this (thus the No).
Now the compiler generates code for placing global variables using the .bss section, which by definition is zeroed, and also explicitly indicates this.
For this
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. var
  4.   S: string;
  5.   N: Integer;
  6. begin
generate
Code: ASM  [Select]
  1. .section .bss
  2.         .balign 8
  3. # [project1.lpr]
  4. # [4] S: string;
  5. U_$P$PROJECT1_$$_S:
  6.         .zero 8
  7.  
  8. .section .bss
  9.         .balign 4
  10. # [5] N: Integer;
  11. U_$P$PROJECT1_$$_N:
  12.         .zero 4
And for global variables of managed types, no additional code is generated for zeroing.
So the compiler knows for sure that global variables are zeroed.

Thaddy

  • Hero Member
  • *****
  • Posts: 5201
Re: Class (static) fields of class. Initial values.
« Reply #12 on: January 05, 2018, 06:53:55 pm »
Sven is correct: this is even now not always the case. E.g. Someone can override newinstance and you are screwed.
You merely demonstrate current behavior. Sven falsified it. (see my autograph)
« Last Edit: January 05, 2018, 06:55:26 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

PascalDragon

  • Jr. Member
  • **
  • Posts: 58
  • Compiler Developer
Re: Class (static) fields of class. Initial values.
« Reply #13 on: January 10, 2018, 11:36:23 am »
Yes and no. Yes: As of now for all targets FPC supports global variables (and static variables) are initialized to 0, but that is not guaranteed by the compiler, but by the OS, so if the OS decides not to do it then it won't be! That is also why we do not document this (thus the No).
Now the compiler generates code for placing global variables using the .bss section, which by definition is zeroed, and also explicitly indicates this.

But we do not guarantee that a .bss section is used for all platforms. Thus global variables are not guaranteed to be initialized to a specific value (and managed variables are only guaranteed a specific value after their initialization code is run).

ASerge

  • Hero Member
  • *****
  • Posts: 536
Re: Class (static) fields of class. Initial values.
« Reply #14 on: January 10, 2018, 03:39:24 pm »
Thus global variables are not guaranteed to be initialized to a specific value (and managed variables are only guaranteed a specific value after their initialization code is run).
Repeat "...And for global variables of managed types, no additional code is generated for zeroing". The compiler (not the OS) knows that it is not required to zeroed them.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus