----- Original Message ----- From: "John Bird" <[EMAIL PROTECTED]> To: "'NZ Borland Developers Group - Delphi List'" <delphi@ns3.123.co.nz> Sent: Wednesday, January 31, 2007 9:44 AM Subject: [DUG] Uninitialised variables/Constants
> Are there any guidelines on when an uninitialised constant or variable can > be expected to have a safe value? Unless you can find something in the language definition (just in the behaviour of a particular compiler isn't enough) then the safest thing and best practice is to treat all variables as if they contained random garbage unless you've initialised them. Constants are just a lexical shorthand - either they exist, and represent the value you've defined for them; or they don't, and you can't refer to them. Pascal particularly, with its roots as a teaching language, has always seemed to take great delight in ensuring uninitialised variables contain the most inconvenient garbage possible. Delphi weakens that stringency considerably; but in general referring to something you haven't initialised still gives an error rather than the safe default null/blank/zero you get in some languages. So, in the case of your particular code posted, I'd say, go ahead and intialise the value, in every record. Even if the current compiler turns out to set it to zero, even if your current code always writes the field before it reads it - you don't need to create the timebomb that'll go off if either of those behaviours changes in the future. You don't need to create that kind of interdependency between code and data. Brian > I am interested particularly in an uninitialised field in a constant record > like: > > type > TSampleRecord = record > SampCode: Integer; > SampName:string[30]; > SampAddress1:array[1..3] of string[20]; > iValue: integer; > end; > > const > ConstArr: array [0..5] of TSampleRecord =( > (SampCode: 0; SampName:'Nought'; > SampAddress1:('A01','A02','A03');iValue: 10), > (SampCode: 1; SampName:'First'; SampAddress1:('A11','A12','A13');iValue: > 11), > (SampCode: 2; SampName:'Second'; > SampAddress1:('A21','A22','A23');iValue: 12), > (SampCode: 3; SampName:'Third'; SampAddress1:('A31','A32','A33');iValue: > 13), > (SampCode: 4; SampName:'Fourth'; > SampAddress1:('A41','A42','A43');iValue: 14), > (SampCode: 5; SampName:'Five missing iValue'; > SampAddress1:('A51','A52','A53')) > ); > > In this case the uninitialised ConstArr[5].iValue behaves as being 0, which > is nice. I am setting up some constant records like this and it would be > convenient if I do not have to explicitly initialise every last field value > especially if I know I won't be using that particular one...what is "best > practice"? > > > John _______________________________________________ Delphi mailing list Delphi@ns3.123.co.nz http://ns3.123.co.nz/mailman/listinfo/delphi