comp.lang.ada
 help / color / mirror / Atom feed
* Assigning to dereferenced null access variable
@ 2000-11-05  0:00 Vincent Marciante
  2000-11-07  0:00 ` Simpler Question (was Assigning to dereferenced null access variable) Vincent Marciante
  0 siblings, 1 reply; 9+ messages in thread
From: Vincent Marciante @ 2000-11-05  0:00 UTC (permalink / raw)


In the following code, I expected a constraint_error to occur
after the line containing "???" but it did not occur.  Is that 
correct behavior?

Also, after that line, I expected the code in the two blocks 
to behave the same but that was not the case.  The following is the output that I get from OS/2 GNAT 3.12

abcd_Sequence.The_Characters.all = "abcd"
Null_Characters(1..0) = Null_Characters
Null_Sequence.The_Characters = null.
Null_Sequence.The_Characters(1..0) = Null_Characters
Null_Sequence.The_Characters(1..0) := Null_Characters(1..0) succeeded ?
Null_Sequence.The_Characters(1..0) := Null_Characters caused Constraint_Error ?


with Ada.Text_IO;  Use Ada.Text_IO;

procedure Null_Sequence_Assignment is

    type Characters is array (Positive range <>) of Character;
    type Reference is access Characters;
    type Character_Sequence is
        record
            The_Length : Natural := 0;
            The_Characters : Reference;
        end record;

    Null_Characters : Characters(1..0);
    abcd_Characters : Characters := "abcd";

    Null_Sequence : Character_Sequence;
    abcd_Sequence : Character_Sequence := (4, new Characters'(1=>'a',2=>'b',3=>'c',4=>'d'));

begin

    abcd_Characters(1..0) := Null_Characters(1..0);
    abcd_Characters(1..0) := Null_Characters;
    Null_Characters       := abcd_Characters(1..0);


    abcd_Sequence.The_Characters(1..0) :=  
        Null_Characters;

    abcd_Sequence.The_Characters(1..Null_Sequence.The_Length) :=
        Null_Sequence.The_Characters(1..Null_Sequence.The_Length);


    abcd_Sequence.The_Characters(1..0) :=
        Null_Sequence.The_Characters(1..0);

    abcd_Sequence.The_Characters(1..0) :=
        Null_Characters;

    put_line("abcd_Sequence.The_Characters.all = """
            & String(abcd_Sequence.The_Characters.all) & '"');  

--- ???

    Null_Sequence.The_Characters(1..0) :=   

      -- Why no Constraint_Error here do to
      -- dereferencing of null access value?
      -- Is it due to allowed optimization?

        abcd_Sequence.The_Characters(1..0);                                      
        


    if Null_Characters(1..0) = Null_Characters then
        Put_Line("Null_Characters(1..0) = Null_Characters");
    else
        Put_Line("Null_Characters(1..0) /= Null_Characters");
    end if;


    if Null_Sequence.The_Characters = null then
        Put_Line("Null_Sequence.The_Characters = null.");
    else
        Put_Line("Null_Sequence.The_Characters /= null.");
    end if;

    if Null_Sequence.The_Characters(1..0) = Null_Characters then
        Put_Line("Null_Sequence.The_Characters(1..0) = Null_Characters");
    else
        Put_Line("Null_Sequence.The_Characters(1..0) /= Null_Characters");
    end if;


    begin
        Null_Sequence.The_Characters(1..0) :=
            Null_Characters(1..0);

            Put_Line("Null_Sequence.The_Characters(1..0) := "
                    &"Null_Characters(1..0) succeeded ?");
            -- I thought that dereferencing The_Characters
            -- would have caused Constraint_Error to occur.
    exception
        when Constraint_error =>
            Put_Line("Null_Sequence.The_Characters(1..0) := "
                    &"Null_Characters(1..0) caused Constraint_Error");
    end;  

    
    begin
        Null_Sequence.The_Characters(1..0) := 
            Null_Characters; 

            Put_Line("Null_Sequence.The_Characters(1..0) := "
                    &"Null_Characters succeeded");
    exception
        when Constraint_error =>
            Put_Line("Null_Sequence.The_Characters(1..0) := "
                    &"Null_Characters caused Constraint_Error ?");
            -- I thought, because Null_Characters(1..0) = Null_Characters,
            -- that the assignment would have succeeded, as it did
            -- in the previous block.
    end;  

end;




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2000-11-10  1:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-05  0:00 Assigning to dereferenced null access variable Vincent Marciante
2000-11-07  0:00 ` Simpler Question (was Assigning to dereferenced null access variable) Vincent Marciante
2000-11-07  0:00   ` Jean-Pierre Rosen
2000-11-07  0:00     ` Vincent Marciante
2000-11-08  4:20       ` Robert Dewar
2000-11-08  0:00         ` Vincent Marciante
2000-11-09  4:43           ` Robert Dewar
2000-11-08  0:00         ` GNAT for OS/2 (was Re: Simpler Question (was Assigning ...)) Vincent Marciante
2000-11-10  1:52           ` tjerick

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox