Default discriminant problem/GNAT compiler bug? 
Author Message
 Default discriminant problem/GNAT compiler bug?


Quote:

> I am working my way through "Programming with Ada95" by J. Barnes and am
> trying to perform the exercises in Chapter 16.2 "Default Discriminants"
> using GNAT 2.07.

> Barnes defines a polynomial type with a default discriminant that serves
> as the size of the array that holds the polynomial coefficients.

>   type polynomial (N:Index := 0) is
>     record
>       A : Integer_Vector(0..N);
>     end record;

> He states that if we make a declaration of

>    P, Q : polynomial;

> without an initial value of a discriminant, then the record is not
> constrained and can be changed by assignment.

> I tried this but noticed that an assignment of an unconstrained record
> CHANGES the values of other local polynomial variables (created either in
> a contrained way or uncontrained way).

> This does not seem logical.  Either
>   1) I don't understand what I am doing (always possbile!)
>   2) Barnes is wrong (not likely)
>   3) there is a bug in the GNAT compiler.

You guessed it!!  It was 1).  My definition of the Index was incorrect and
causing the compiler to grab too much space.  Bonehead error!!  GNAT and
Barnes are safe.

--
Michael Bartz
The University of Memphis




Sun, 17 May 1998 03:00:00 GMT  
 Default discriminant problem/GNAT compiler bug?
I am working my way through "Programming with Ada95" by J. Barnes and am
trying to perform the exercises in Chapter 16.2 "Default Discriminants"
using GNAT 2.07.

Barnes defines a polynomial type with a default discriminant that serves
as the size of the array that holds the polynomial coefficients.

  type polynomial (N:Index := 0) is
    record
      A : Integer_Vector(0..N);
    end record;

He states that if we make a declaration of

   P, Q : polynomial;

without an initial value of a discriminant, then the record is not
constrained and can be changed by assignment.

I tried this but noticed that an assignment of an unconstrained record
CHANGES the values of other local polynomial variables (created either in
a contrained way or uncontrained way).

This does not seem logical.  Either
  1) I don't understand what I am doing (always possbile!)
  2) Barnes is wrong (not likely)
  3) there is a bug in the GNAT compiler.

All the essentials are below:

Help!

Michael Bartz
The University of Memphis

-- test program for polynomials
-- Michael Bartz

with poly; use poly;
with text_io; use text_io;

procedure test is
   package int_io is new integer_io(integer);
   use int_io;

   P : Polynomial := (3,(5,0,4,2));
   Q : Polynomial := (1,(0,1));
   Z : Polynomial;
begin
   Z := (4, (5, 5, 5, 5, 5));
   put("Z = ");
   poly.print(Z);

   put("Z.N = "); put(Z.N); new_line;
   put("Q = ");
   poly.print(Q);
   put("P = ");
   poly.print(P);
end test;

OUTPUT:

Z = Degree = (4)  5           5           5           5           5
Z.N =           4
Q = Degree = (5)  5           5           5           1  -277148592           3
P = Degree = (3)  5           0           4           2

-- Specs for the polynomial package
-- Michael Bartz

package poly is

  subtype Index is Integer range 0..Integer'Last;
  type Integer_Vector is array(Index range <>) of Integer;

  type polynomial (N:Index := 0) is
    record
      A : Integer_Vector(0..N);
    end record;

  procedure print(P : Polynomial);

end poly;

--
Michael Bartz
The University of Memphis




Sun, 17 May 1998 03:00:00 GMT  
 Default discriminant problem/GNAT compiler bug?

"You guessed it!!  It was 1).  My definition of the Index was incorrect and
causing the compiler to grab too much space.  Bonehead error!!  GNAT and
Barnes are safe."

Michael you are not being fair to yourself :-)

First, it is not necessarily the case that an Ada compiler will allocate
the maximum space in such cases, some compilers prefer to make hidden use
of the heap. I prefer the maximum space approach (as does Tuck I believe,
and as does the Dec compiler), because I don't think a compiler has any
business doing dynamic allocation behind your back.

Second, GNAT should really have signalled a storage error instead of
tromping over your data. This is on our list of things to do for
version 3.02.

By the way, 3.01 is now frozen at our site for release, and we are
expecting releases to start to appear today or tomorrow. A lot has happened
since 2.07, the NYU/ACT changeover, validation, many new features, and
over 500 filed bugs reports fixed. We think 3.01 should be pretty stable,
but in any case we are planning a followup 3.02 (which will mostly be
additional bug fixes) relatively soon.



Sun, 17 May 1998 03:00:00 GMT  
 Default discriminant problem/GNAT compiler bug?
Regarding:

package poly is

  subtype Index is Integer range 0..Integer'Last;
  type Integer_Vector is array(Index range <>) of Integer;

  type polynomial (N:Index := 0) is
    record
      A : Integer_Vector(0..N);
    end record;

  procedure print(P : Polynomial);

end poly;

the problem here is that the type polynomial requires a lot of space
for unconstrained objects (about 8 gigabytes). Now of course GNAT should
generate a storage error for this case, but it does not at the moment,
because stack checking is not fully implemented, actually the problem is
really more general, objects whose size exceeds 2**32 bytes are not
properly caught in all cases (they are in some, but not all, including
this one).

Your program will work fine if you change Integer'Last to a reasonable value.

If you had used Natural instead of Index for the type, GNAT would have been
a little more helpful:

     1. package poly is
     2.   type Integer_Vector is array(Natural range <>) of Integer;
     3.   type polynomial (N:Natural := 0) is
     4.     record
     5.       A : Integer_Vector(0..N);
                                    |
        >>> warning: creation of object of this type may raise Storage_Error

     6.     end record;
     7.   procedure print(P : Polynomial);
     8. end poly;

Clearly we need to broaden the check for this message!

P.S. I am answering this on comp.lang.ada because this is a very common

there too :-)



Sun, 17 May 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Discriminant as default initial value

2. Discriminant as default initial value

3. GNAT's internal format for Discriminant Records?

4. GNAT Compiler-Bug?

5. Bug in GNAT.Socket.Check_Selector in GNAT 3.14 Linux runtime

6. GNAT bug or expectations problem with Text_Io.Text_Streams

7. problem solved, but I don't know why (was: GNAT implementation bug)

8. Win32.Commdlg.GetOpenFileName problem - Gnat bug?

9. GNAT compiler problem?

10. gnat compiler problems on linux

11. gnat304 Daylight Saving Time problem (was: compiler errors in GNAT 304a (win95))

12. problem compiling tclgdbm (compiler bug?)

 

 
Powered by phpBB® Forum Software