comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: A smaller self contained test case. Was: Compiler Bug or what I'm doing wrong?
Date: Sat, 24 Jun 2006 21:58:31 +0200
Date: 2006-06-24T21:58:34+02:00	[thread overview]
Message-ID: <pdjnhfm4tk1l.17h7tysjkfw82.dlg@40tude.net> (raw)
In-Reply-To: bfy7vmpsmb.fsf@hod.lan.m-e-leypold.de

On 24 Jun 2006 15:53:32 +0200, M E Leypold wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> 
>> BTW, I wouldn't use discriminants with defaults, 
> 
> I'm perhaps misuing dicriminants anyway in that application. But they
> need to have defaults, since the disriminated records are fields in
> another record. Like
> 
>   type All_We_Know ( Internal_Data_Available : Boolean := True ) is record 
>     case Internal_Data_Available is
>          when True  =>  Secret_Data : ... ;
>          ...
>   end record;
> 
>   type ... is record 
>     Things_Known : All_We_Konw;
>     ...
>   end record;
>  
> (Ok, stupid example, but you get ist: Data not to be given to an
>  external data processing application can be supressed by the
>  discriminant).

All_We_Know looks much like a handle. Then I'd try to make it more OO, if
you aren't allergic to it. What about tagged types? (:-))

>> and in any case, would use
>> Input for them. 
> 
> I tried to do that, but that didn't fix the problem. I also wouldn't
> expect that: According to Barnes Input most probably uses Read.

Or a part of it. Anyway, try this:

with Ada.Text_IO;
with Ada.Streams.Stream_IO;  use Ada.Streams.Stream_IO;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;

procedure Bug3 is

  type Customer_Description (Is_Company : Boolean := True) is record
     case Is_Company Is
        when True =>
           Foo          : Integer := 0;       -- remove these and the
           Bar          : Integer := 0;       -- SIGSEGV goes away
           Company_Name : Unbounded_String ;
        when False =>
           Persons_Name : Unbounded_String;
        when others  =>  null;
     end case;
  end record;

  procedure Set_Zugang (D : in out Customer_Description; Z : Boolean) is
  begin
     if Z /= D.Is_Company then
        case Z is
           when True =>
              declare
                 New_D : Customer_Description (True);
              begin
                 D := New_D;
              end;
           when False  =>
              declare
                 New_D : Customer_Description (False);
              begin
                 D := New_D;
              end;
        end case;
     end if;
  end;

  procedure Write_Record is
     F : File_Type;
     S : Stream_Access;
     R : Customer_Description;
  begin
     Create (F, Name => "tmp-bug3");
     S := Stream (F);
     Set_Zugang (R, False);                   -- remove this and the
SIGSEGV goes away

     for I in 1 .. 400 loop
        Customer_Description'Output (S,R);     -- [1a]
     end loop;
     Close (F);
  end;

  procedure Get_Record is
     F : File_Type;
     S : Stream_Access;
  begin
     Open (F, Mode => In_File, Name => "tmp-bug3" );
     S := Stream (F);
     for I in 1 .. 400 loop
        declare
           R : Customer_Description renames
                  Customer_Description'Input (S);
        begin
           Ada.Text_IO.Put_Line ("Read" & Integer'Image (I));
        end;
     end loop;
     Close (F);
  end;

begin
  Write_Record;
  Get_Record;
end;

It should work with GNAT 3.15p / Windows.

>> 1. You have the discriminant stored anyway (that was the choice about
>> having the defaults), so you cannot use one stored value of the
>> discriminant for all 400 fields, even if they had it same.
> 
> Oops. That I don't understand.

If the discriminant had no default, then Write would not store it. So if
all 400 elements had the same value of the discriminant, you could first
store its value manually (Boolean'Write) and then use 'Write for each
element. When reading you'd read the discriminant value once and then use
Read on a variable constrained to that value:

type Foo (C : Boolean) is ...;

Boolean'Write (S, False);
for I in ... loop
   Foo'Write (S, Get_R (I)); -- I know that all C's are same
end loop;

declare
   C : Boolean;
begin
   Boolean'Read (S, C);
   declare
      R : Foo (C);
   begin
      for I in ... loop
         Foo'Real (S, R); -- I know that all C's are same
      end loop;

>> 2. Read means that you have one extra initialize / finalize pair. Input
>> does not:
> 
> I don't care. Input is slow anyway and doesn't happen so often :-).

So it is safe to use it, at least to work the bug around.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2006-06-24 19:58 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-20 16:56 Compiler Bug or what I'm doing wrong? M E Leypold
2006-06-21  7:44 ` Ludovic Brenta
2006-06-21 12:29   ` M E Leypold
2006-06-21 12:46     ` Alex R. Mosteo
2006-06-21 13:23       ` M E Leypold
2006-06-22 19:10         ` Simon Wright
2006-06-23  8:24         ` Ludovic Brenta
2006-06-23 13:14         ` Alex R. Mosteo
2006-06-23 13:24           ` Alex R. Mosteo
2006-06-24 20:33             ` Simon Wright
2006-06-24 20:56               ` M E Leypold
2006-06-26  7:32                 ` Ludovic Brenta
2006-06-26 11:16                   ` M E Leypold
2006-06-26 12:13                     ` [Ada in Debian] GtkAda and GNAT versions Ludovic Brenta
2006-06-26 12:25                       ` M E Leypold
2006-06-27 20:55                   ` Compiler Bug or what I'm doing wrong? Simon Wright
2006-06-27 22:26                     ` Ludovic Brenta
2006-06-22  2:07       ` James Dennett
2006-06-22  6:37         ` Duncan Sands
2006-06-22 16:53           ` M E Leypold
2006-06-22 19:01             ` Pascal Obry
2006-06-23  8:37               ` M E Leypold
2006-06-22 19:05             ` Dmitry A. Kazakov
2006-06-23  4:47               ` Jeffrey R. Carter
2006-06-23 12:26               ` Stephen Leake
2006-06-23 13:11                 ` Dmitry A. Kazakov
2006-06-23 13:15                 ` Alex R. Mosteo
2006-06-23  9:55 ` A smaller self contained test case. Was: " M E Leypold
2006-06-23 10:03   ` M E Leypold
2006-06-23 11:04   ` And a Workaround: Was: A smaller test case / Compiler Bug M E Leypold
2006-06-23 11:12     ` Possible memory leaks when reading/writing variant records M E Leypold
2006-06-24 11:46   ` A smaller self contained test case. Was: Compiler Bug or what I'm doing wrong? Dmitry A. Kazakov
2006-06-24 12:27     ` M E Leypold
2006-06-24 12:52       ` Dmitry A. Kazakov
2006-06-24 13:53         ` M E Leypold
2006-06-24 19:58           ` Dmitry A. Kazakov [this message]
2006-06-24 20:22             ` M E Leypold
2006-06-25  7:59               ` Dmitry A. Kazakov
2006-06-25 10:51                 ` M E Leypold
2006-06-26  6:22                   ` Martin Dowie
2006-06-24 21:21             ` M E Leypold
2006-06-25 21:36   ` M E Leypold
2006-06-26 21:53   ` Possibly fixed in gcc 4.1.1, but bug box -- Was: Re: A smaller self contained test case M E Leypold
2006-06-27 18:24     ` Alex R. Mosteo
2006-06-27 22:58       ` M E Leypold
2006-06-28 10:32         ` Alex R. Mosteo
2006-07-03  1:38         ` Steve Whalen
2006-07-03 10:36           ` M E Leypold
2006-06-28  8:41       ` Ludovic Brenta
2006-06-28  8:51         ` Georg Bauhaus
2006-06-28 10:43         ` Alex R. Mosteo
2006-06-23 10:00 ` Compiler Bug or what I'm doing wrong? M E Leypold
replies disabled

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