[fpc-devel] Are global variables guaranteed to be zero?

Frank Church vfclists at gmail.com
Wed Feb 20 20:53:47 CET 2013


On 29 November 2012 10:08, Alexander Klenin <klenin at gmail.com> 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.
>
> I have rather curious story behind this request.
> In Russia, schoolchildren must pass "Unified State Exam" ("ЕГЭ" in
> russian) upon graduation.
> The exam on informatics includes requirement to write a simple program.
> Currently, the program is allowed to be written in any programming language,
> but it is written on paper, the pupil must precisely specify the language,
> and the program is graded manually by teachers.
>
> Some pupils wrote in Free Pascal, which is moderately popular in schools
> (something around 20% IIRC). Several of them omitted initialization of
> global arrays
> based on the assumption that they will be zeroed automatically.
> Those pupils were failed for that, and the graders stated that even if
> current implementation
> happens to zero global variables, this is not documented and so is merely
> an implementation artifact which must not be relied upon.
>
> Hence, this omission resulted in lower grades for some schoolchildren.
>
> --
> Alexander S. Klenin
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel

The teachers were correct. A program's correctness stands purely on
the states defined by the meaning of its texts, not on some compiler
behaviour or some execution or implementation model.  The teachers are
at fault here. I don't suppose they understand that themselves, as the
students wouldn't err if the teachers impressed that principle before
the test. The graders were right but for the wrong reasons.

In the pure sense defined by the meaning of the texts the initial
states are undefined if the variables are not expressly initialized.

"We must learn to work with program texts while (temporarily) ignoring
that they admit the interpretation of executable code."
http://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1036.html.
As some of you might suspect I am a fan of the late Edsger Dijkstra,
That paper is a bit of a (quite justified) rant but some of the issues
are covered in his other papers, but it has taken me some time to
begin to grok the foundations of his perspective.

I am also not that good a programmer. :-(


-- 
Frank Church

=======================
http://devblog.brahmancreations.com



More information about the fpc-devel mailing list