* * *

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

Thaddy

  • Hero Member
  • *****
  • Posts: 6371
Re: Class (static) fields of class. Initial values.
« Reply #15 on: January 10, 2018, 04:12:49 pm »
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.
What about stack? Stackspace is dirty by default.What about longstrings that are part of a class or record? What about code where the initialization is overridden (e.g. newinstance is virtual)? You are making assumptions that are nor always true.
And it is not always the compiler, but the runtime library that initializes memory through InitInstance, which in turn can be skipped in newinstance.
This is only the scenario for classes. With managed objects and managed records - that are now supported - it is even more dangerous to rely on such an assumption: e.g. New() does getmem (dirty), not allocmem(clean).

You should assume *nothing* about initialization and even ignore some current behavior that you happen to think to know about the current implementation.

BTW: This is also true for Delphi: at least before XE2 and higher, compiling.debugging in the IDE cleans all heap memory, but not when running a program outside the IDE: behavior is not to clean the heap unless initinstance (which happens to AllocMem..implementation detail)is called, and that is not guaranteed..
Quote
The compiler (not the OS) knows that it is not required to zeroed them.
Is therefor nonsense. This is a programmer's task, not a compiler's task. Class fields or not: they can not be assumed to be clean.
« Last Edit: January 10, 2018, 04:51:03 pm by Thaddy »
I am enjoining wine, not whine....

PascalDragon

  • Full Member
  • ***
  • Posts: 131
  • Compiler Developer
Re: Class (static) fields of class. Initial values.
« Reply #16 on: January 13, 2018, 01:50:51 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.
But the compiler would generate such code if we had a target that does not support something akin to .bss. Case in point is that it does generate initialization calls for records with management operators cause it does not know what the code in there does with the record. For the other primitive types the compiler knows that they are to be initialized to 0/Nil and thus does not need to do anything on a target that does support .bss.

 

Recent

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