[fpc-devel] Are global variables guaranteed to be zero?
michael.vancanneyt at wisa.be
michael.vancanneyt at wisa.be
Thu Nov 29 13:03:37 CET 2012
On Thu, 29 Nov 2012, Mark Morgan Lloyd wrote:
> michael.vancanneyt at wisa.be wrote:
>> On Thu, 29 Nov 2012, Alexander Klenin wrote:
>>
>>> On Wed, Nov 28, 2012 at 2:29 PM, Jonas Maebe <jonas.maebe at elis.ugent.be>
>>> wrote:
>>>>
>>>>> Will global variables and static global arrays be always initialized to
>>>>> zero?
>>>>
>>>> Yes.
>>>
>>> Then I suggest to amend the first paragraph of
>>> http://www.freepascal.org/docs-html/ref/refse22.html
>>> which directly contradicts this.
>>
>> It does not directly contradict this ?
>>
>> For local variables the statement is 100% correct.
>> You must initialize local variables.
>
> Are there cases where locals are set to a sane initial state, e.g. for
> strings and dynamic arrays? What about (references to) objects?
Managed types are normally initialized. That means Ansistrings,
UnicodeString, and COM interfaces and dynamic arrays (maybe I forget some)
Classes and objects are not. I am not sure about widestrings on Windows.
But again, not always:
For instance
Function a(B : Integer) : Ansistring;
begin
Result:=Result+' something';
end;
You would think that Result is initialized because it is managed:
it is an ansistring. In fact, it is not initialized, leading sometimes to surprises.
I only learned about this relatively recently, much to my surprise.
It is one of the reasons I am reluctant to document this in detail, there
always seem to pop up new cases.
In general, you are safer off by assuming that nothing is initialized.
Initializing it again does no damage.
Michael.
More information about the fpc-devel
mailing list