Recent

Author Topic: Initialization of static class fields  (Read 1452 times)

avk

  • Hero Member
  • *****
  • Posts: 752
Initialization of static class fields
« on: January 30, 2020, 06:57:34 am »
I did not find any mention in the documentation about the initialization of static class fields. Are the rules the same as for global variables?

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Initialization of static class fields
« Reply #1 on: January 30, 2020, 08:49:36 am »
I believe that indeed static class fields are initialized. But that is by testing and not by documentation.
IOW I can verify, but have not seen a falsification example nor can I come up with one.
Did you ask on the developers list?
« Last Edit: January 30, 2020, 08:53:18 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Initialization of static class fields
« Reply #2 on: January 30, 2020, 08:54:25 am »
Or do you mean initialize them with your own values? You can do that in the initialize section of your unit.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)


Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Initialization of static class fields
« Reply #4 on: January 30, 2020, 09:09:18 am »
Or do you mean initialize them with your own values? You can do that in the initialize section of your unit.
Well that's the wrong place. It should be done in the class constructor. Although your suggestion works it is inconsistent design.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Initialization of static class fields
« Reply #5 on: January 30, 2020, 10:25:03 am »
Did you ask on the developers list?
Not yet, I still hope that @PascalDragon will see this question.

Thanks to @julkas, I saw a topic two years old, which states:
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).
However, current documentation states:
Quote
  ...
  Remark

   2 exceptions to this rule exist:
    1. Managed types are an exception to this rule: Managed types are always initialized with a default value: 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 223
    2. Global variables are initialized with the equivalent of zero.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Initialization of static class fields
« Reply #6 on: January 30, 2020, 11:19:00 am »
Nearly all normal OSes zero out newly given pages for security reasons. They usually have some background process  (kernel thread) to zero pages that are recycled.

However with archaic or embedded systems this can be different. That is what the "guarantee" bit is about.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Initialization of static class fields
« Reply #7 on: January 30, 2020, 11:37:46 am »
@marcov, thank you very much.

 

TinyPortal © 2005-2018