|
Prev: Converting Type Characters to type string
Next: Are there noticable differences in Ada acceptance by country?
From: ma740988 on 30 Mar 2008 16:58 Well folks its been at least 4 years since I've perused and/or written Ada source. That aside I'm perusing source code written in Ada. So consider: -- used to restrict a variable within a range -- for example: -PI to +PI type Restrict_Function is access function (X: Real4) return Real4; type Filt_Data is record Restrict_Func : Restrict_Function ; end record ; -- within a procedure we have Fdata : Filt_Data; Restrict_Func : Restrict_Function := FData.Restrict_Func ; if Restrict_Func /= null then -- stuff endif At issue: I'm not following the conditional logic 'if (Restrict_Func / = null)'. Not understanding the impetus behind the check for null.
From: jimmaureenrogers on 30 Mar 2008 17:34 On Mar 30, 2:58 pm, ma740...(a)gmail.com wrote: > Well folks its been at least 4 years since I've perused and/or written > Ada source. That aside I'm perusing source code written in Ada. So > consider: > > -- used to restrict a variable within a range > -- for example: -PI to +PI > type Restrict_Function is access function (X: Real4) return Real4; > > type Filt_Data is > record > Restrict_Func : Restrict_Function ; > > end record ; > > -- within a procedure we have > Fdata : Filt_Data; > Restrict_Func : Restrict_Function := FData.Restrict_Func ; > > if Restrict_Func /= null then > -- stuff > endif > > At issue: I'm not following the conditional logic 'if (Restrict_Func / > = null)'. Not understanding the impetus behind the check for null. Type Restrict_Function is an access type. It is possible for an instance of Restrict_Function to have a null value. This kind of problem can occur if you are creating an array of Restrict_Function and only some of the elements of the array are set to non-null values. You will encounter a run-time error if you try to dereference a null access value. Jim Rogers
From: Ludovic Brenta on 30 Mar 2008 17:36 ma740988(a)gmail.com writes: > I'm not following the conditional logic 'if (Restrict_Func / = > null)'. Not understanding the impetus behind the check for null. If Restrict_Func is null and you try to call the subprogram it designates, you get a Constraint_Error (ARM 4.9(13)). If you disable runtime checks, your program is likely to crash. -- Ludovic Brenta.
From: Robert A Duff on 30 Mar 2008 17:37 ma740988(a)gmail.com writes: > Well folks its been at least 4 years since I've perused and/or written > Ada source. That aside I'm perusing source code written in Ada. So > consider: > > -- used to restrict a variable within a range > -- for example: -PI to +PI > type Restrict_Function is access function (X: Real4) return Real4; > > type Filt_Data is > record > Restrict_Func : Restrict_Function ; > > end record ; > > > -- within a procedure we have > Fdata : Filt_Data; > Restrict_Func : Restrict_Function := FData.Restrict_Func ; > > if Restrict_Func /= null then > -- stuff > endif > > At issue: I'm not following the conditional logic 'if (Restrict_Func / > = null)'. Not understanding the impetus behind the check for null. Restrict_Func is a pointer to a function. To call the function, you say something like "Restrict_Func.all(X)". You can leave the ".all" implicit: "Restrict_Func(X)", but the ".all" still happens. ".all" on a null pointer raises an exception. In your example, presumably "stuff" calls the function -- so it needs to be protected by the 'if'. It's usually better to declare: type Restrict_Function is not null access function ...; ^^^^^^^^ so you don't need to worry about null, which is just a tripping hazard. Or use primitive operations of tagged types and dispatching calls. - Bob
From: Georg Bauhaus on 30 Mar 2008 17:44
On Sun, 2008-03-30 at 13:58 -0700, ma740988(a)gmail.com wrote: > -- used to restrict a variable within a range > -- for example: -PI to +PI > type Restrict_Function is access function (X: Real4) return Real4; > > type Filt_Data is > record > Restrict_Func : Restrict_Function ; > > end record ; > > > -- within a procedure we have > Fdata : Filt_Data; > Restrict_Func : Restrict_Function := FData.Restrict_Func ; > > if Restrict_Func /= null then > -- stuff > endif > > At issue: I'm not following the conditional logic 'if (Restrict_Func / > = null)'. Not understanding the impetus behind the check for null. The type Restrict_Function includes the null value. The FData.Restrict_Func record component gets the default initial value for access values. It is therefore null. So Filt_Data.Restrict_Func is actually null. |