[fpc-pascal] Re: classes initialization

L L at z505.com
Wed Mar 26 00:10:37 CET 2008


 >Using uninitialised variables is virtually always bad, regardless of the
 > scope. And the fact that
 > global variables are zeroed at the program start is afaik not defined 
 > by the Pascal standard. It's
 > just a side effect of the way most operating systems work.

I think something like:

procedure test;
var blah: integer = 0;
begin
end;

Is useful.. i.e. initialized variables that are done on the spot.

And similarily a record could be:

procedure test;
var blah: TRec autoinit;
begin
end;

This would allow safe programming without compromising backwards 
compatibility.

But... in the mean time.. a solution is to make a standard record init 
procedure and do it like:

procedure test;
var blah: TRec;
begin
  init(blah);
end;

Which will be what I use for now...

And always do this by habit for safety now that I know this trick of the 
fillchar and such - only ever avoiding it when needing speed (but that 
is premature optimization).

I realize that local scope uninitialized vars are the way systems work 
according to the system.. but we are humans and error prone humans...We 
can make languages safer I think and more consistent and human friendly 
at a higher level.  Humans just make so many mistakes even though 
systems are supposed to work they way they are.

i.e. really it is just a convenience and a "standard good practice" 
being now recommended right within the programming language...and more 
safety as an option. Just as I am also a fan of reference counted 
ansistrings versus using less safe pchars.. etc.



More information about the fpc-pascal mailing list