Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GNAT reject an array type with a default discriminant value?

Tags:

ada

This (declaration) code raises a storage error:

type Vector is array (Integer range <>) of Integer;
type Array_Starting_At_One (Max : Integer := 0) is
   record
      Mat : Vector (1 .. Max);
   end record;

X : Array_Starting_At_One;

I can't figure out why. If I specify the default, as in X : Array_Starting_At_One (Max => 0); the error disappears, although the Array_Starting_At_One type specification still triggers a warning that creation of such objects may raise Storage_Error.

I am not even trying to store a single bit, so this error doesn't make any sense to me:

raised STORAGE_ERROR : System.Memory.Alloc: object too large
like image 318
TamaMcGlinn Avatar asked Oct 22 '20 12:10

TamaMcGlinn


People also ask

What are the characteristics of discriminant algebra?

The formula of discriminant algebra exhibits the following characteristics - When discriminant is zero, it shows that there are repeated real number solution to the quadratic; For a negative discriminant, neither of the solutions amount to real numbers;

What is the relationship between discriminant and roots?

The relationship between discriminant and roots can be understood from the following cases – Then, the roots of the quadratic equation are real and unequal. Then, the roots of the quadratic equation are real and equal. Then, the roots of the quadratic equation are not real and unequal.

What is the root of discriminant quadratic function?

This root pertains to the value represented by ‘x’. What is Discriminant Quadratic Function? Within this quadratic formula, the discriminant function is found under a quadratic formula. It is represented as b²-4ac and the discriminant can be zero or positive or negative.

What does it mean when the discriminant is zero?

When discriminant is zero, it shows that there are repeated real number solution to the quadratic; For a negative discriminant, neither of the solutions amount to real numbers; For a positive discriminant, there are two distinct real number solutions to the quadratic equation.


Video Answer


3 Answers

When a variable is declared using the default discriminant, then the discriminant can later be changed via an assignment. This means that the compiler (at least GNAT does this) will allocate (on the stack) enough room to hold an object with any discriminant up to the maximum allowed value (Integer'Last in this case).

Either increase your stack size (not neccessarily recommended), or use a different subtype more suited to your problem:

subtype Foo is Integer range 1..10;
type Vector is array (Foo range <>) of Integer;
like image 81
egilhh Avatar answered Oct 21 '22 21:10

egilhh


Certainly any array with an index range of 1..Integer'Last could raise Storage_Error. The compiler is warning you of this possibility. Try restricting the index range for your array type such as:

subtype Indices is Integer range 0..1024;
type Vector is Array (Indices range <>) of Integer;
type Array_Ending_At (Max : Indices := 0) is
   record
      Mat : Vector(1..Max);
   end record;
like image 44
Jim Rogers Avatar answered Oct 21 '22 20:10

Jim Rogers


As you notice, this is a compiler issue. The Janus/Ada compiler would accept your code without complaint or run-time exception.

Your variable X is an unconstrained object; the discriminant (and so the size of the object) can be changed by full-record assignment. The ARM is silent about how such things should be implemented. GNAT has chosen to allocate enough space for the largest possible value; since X is allocated on the stack, there will not be enough space for it with GNAT's default stack size (if you allocate it on the heap, you might not have a problem). Janus instead allocates only enough space for the current value, which may result in X.Mat being implicitly allocated on the heap.

The Janus way is more developer-friendly and acceptable for most applications, where the developer doesn't care where things are allocated, but there are situations where implicit heap allocation cannot be accepted, so the GNAT way is more general.

like image 44
Jeffrey R. Carter Avatar answered Oct 21 '22 21:10

Jeffrey R. Carter