* Re: problem in changing Discriminants from access type
1998-05-12 0:00 problem in changing Discriminants from access type Gil Kaspi
@ 1998-05-11 0:00 ` Lowe Anthony A
1998-05-11 0:00 ` Matthew Heaney
1998-05-11 0:00 ` Matthew Heaney
1998-05-12 0:00 ` Anonymous
2 siblings, 1 reply; 5+ messages in thread
From: Lowe Anthony A @ 1998-05-11 0:00 UTC (permalink / raw)
Did the compiler tell which line would cause the constraint error? On the first
declaration of the discriminant you have start at 1 and end at 0 resulting in
(1..0) which is definiatly a constraint error.
Tony
Gil Kaspi wrote:
> this program was build to simulate dynamic array .
> (it should be a package that has
> type arr is private; and arr defenition is access to arr_node)
> ----------------------------------------------------------------------------
> -----------------
>
> procedure d_array is
>
> type Arr_Node;
>
> type arr is access arr_node;
>
> type Test is array (Integer range <>) of Integer;
>
> subtype Int_Limits is Integer range -1000..1000;
>
> type Int_Arr is array (Int_Limits range <>) of Integer;
>
> type Arr_Node (Start : Int_Limits :=1; Ends : Int_Limits := 0) is
> record
> Store : Int_Arr (Start..Ends);
> end record;
>
> -- declare --
> D_Arr : Arr;
>
> begin
>
> -- create --
> D_Arr := new Arr_Node'(Start => 1, Ends => 0, Store => (others => 0));
>
> -- change --
> D_Arr.all :=(Ends => Index, Start => Index , Store => (others =>1));
>
> end;
> ----------------------------------------------------------------------------
> ------------------------
>
> !!! first, writing this code without the access will work OK.
> running this code will raise CONSTRAINT_ERROR.
> the problem I think is hiding in the last line
>
> 10x 4 any1 that will help
> gil.
--
Tony Lowe Rockwell Collins
1431 Opus Place - Downers Grove, IL 60515
(630)-960-8603 Fax : (630)-960-8207
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: problem in changing Discriminants from access type
1998-05-11 0:00 ` Lowe Anthony A
@ 1998-05-11 0:00 ` Matthew Heaney
0 siblings, 0 replies; 5+ messages in thread
From: Matthew Heaney @ 1998-05-11 0:00 UTC (permalink / raw)
In article <35577DC2.1A8D482B@cacd.rockwell.com>, aalowe@cacd.rockwell.com
wrote:
(start of quote)
Did the compiler tell which line would cause the constraint error? On the first
declaration of the discriminant you have start at 1 and end at 0 resulting in
(1..0) which is definiatly a constraint error.
(end of quote)
No. The index range 1 .. 0 designates a null range, and so the array
object is null.
The rule was carefully designed to *prevent* Constraint_Errors. The rule
is that, for a null range, you're allowed to violate the constraints of the
subtype (though not of the base type). For example,
1 .. 0
-1001 .. -1002
1002 .. 1001
are all legal index ranges. Note that the latter two ranges aren't even in
the Int_Limits subtype. This is perfectly legal, and will NOT raise CE.
That's why you can say
S : String (1 .. 0);
even though 0 is outside the index subtype of type String (Positive). It's
the reason why
S : String (-1 .. -527);
is legal too, and won't raise CE.
This rule is designed so you won't have to litter your code with special
tests for a null slice.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: problem in changing Discriminants from access type
1998-05-12 0:00 problem in changing Discriminants from access type Gil Kaspi
1998-05-11 0:00 ` Lowe Anthony A
@ 1998-05-11 0:00 ` Matthew Heaney
1998-05-12 0:00 ` Anonymous
2 siblings, 0 replies; 5+ messages in thread
From: Matthew Heaney @ 1998-05-11 0:00 UTC (permalink / raw)
In article <35577418.0@news1.ibm.net>, "Gil Kaspi" <kaspi@ibm.net> wrote:
(start of quote)
procedure d_array is
type Arr_Node;
type arr is access arr_node;
type Test is array (Integer range <>) of Integer;
subtype Int_Limits is Integer range -1000..1000;
type Int_Arr is array (Int_Limits range <>) of Integer;
type Arr_Node (Start : Int_Limits :=1; Ends : Int_Limits := 0) is
record
Store : Int_Arr (Start..Ends);
end record;
-- declare --
D_Arr : Arr;
begin
-- create --
D_Arr := new Arr_Node'(Start => 1, Ends => 0, Store => (others => 0));
-- change --
D_Arr.all :=(Ends => Index, Start => Index , Store => (others =>1));
end;
----------------------------------------------------------------------------
------------------------
!!! first, writing this code without the access will work OK.
running this code will raise CONSTRAINT_ERROR.
the problem I think is hiding in the last line
(end of quote)
The reason you're getting CE is because you're trying to change the
discriminant of an aliased object (here, on the heap). This is illegal.
Here's why.
Consider this:
type R (D : Boolean := True) is
record
case D is
when False =>
F : Float;
when True =>
I : Integer;
end case;
end record;
type RA is access R;
subtype RAF is RA (False);
subtype RAT is RA (True);
declare
O1 : constant RA := new RA'(True, I => 5);
O2 : constant RAT := O1;
begin
<O1 and O2 both designate an object whose discrim is True>
O1.all := (False, F => 5.0); -- (illegal)
<O1 designates an object whose discrim is False, but what about O2???>
You see the problem. By declaring an access subtype, you're guaranteeing
something about the object designated by the access object. Here, O2,
because it is of subtype RAT, has a guarantee that it will always point to
an object whose discriminant is True.
If you were able to assign the object a new value with a different
discriminant (through an unconstrained access type, like RA), then that
would violate the invariant of all the access objects designating the
object with the original discriminant.
This rule also applies to statically declared objects (ie not on the heap)
that are declared aliased:
declare
O : alised R;
begin
The statment
O := (False, 5.0);
is illegal, because it changes the value of the discriminant from True to False.
However,
declare
O : R;
begin
O := (False, 5.0);
is perfectly legal, and will not raise CE.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: problem in changing Discriminants from access type
1998-05-12 0:00 problem in changing Discriminants from access type Gil Kaspi
1998-05-11 0:00 ` Lowe Anthony A
1998-05-11 0:00 ` Matthew Heaney
@ 1998-05-12 0:00 ` Anonymous
2 siblings, 0 replies; 5+ messages in thread
From: Anonymous @ 1998-05-12 0:00 UTC (permalink / raw)
On Tue, 12 May 1998 00:56:15 +0200, "Gil Kaspi" <kaspi@ibm.net> wrote
asking about changing the discriminants of a value designated by an
access value. Both the responses I have seen appear to be wrong, though
in different ways.
The correct reason that this raises Constraint_Error is given in ARM
3.10 (9): "Similarly, if the object created by an allocator has
discriminants, the object is constrained, either by the designated
subtype, or by its initial value."
Thus, the object created by "new" is constrained, so its discriminant(s)
can not change. Attempting to change the discriminant(s) is an attempt
to violate a constraint, and so raises Constraint_Error, as Gil Kaspi
has discovered.
ARM 3.10(9) tells us something else useful about discriminants and being
constrained: all aliased objects are constrained.
Jeff Carter PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"Son of a window-dresser."
Monty Python & the Holy Grail
Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/
^ permalink raw reply [flat|nested] 5+ messages in thread