# Compiler storage strategy for mutable discriminated types

**URL:** <https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979>\
**Category:** Help\
**Created:** [November 22, 2026, 8:33pm UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979 "2026-11-22T20:33:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [November 22, 2026, 8:33pm UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979/1 "2026-11-22T20:33:44Z")

</div>

Hello,

I am reading Barnes’s book and after reading the part on discriminants I have come to wonder how does the compiler approach the storage of discriminated types with default discriminant (hence mutable). I understand how if they are constrained on declaration they can be stored easily, since then the storage is known. However, in the case of being unconstrained, the only way I can think of is having it dynamically allocated or otherwise reserving the maximum space possible in the stack given the discriminant, which could be very large such as in the following example.

```Ada
type Maybe_Big_Record (N : Natural := 0) is
   record
      My_Name : String (1 .. N);
   end record;

```

Best regards.  
zen

---

<div class="post-metadata">

**Author:** ![pmnw](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/p/c37758/32.png) [@pmnw](https://forum.ada-lang.io/u/pmnw)\
**Post date:** [November 23, 2026, 1:34am UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979/2 "2026-11-23T01:34:00Z")

</div>

First of all, you forgot to write the default discriminant in your type declaration.

As for what happens, this may depend on the compiler, so I can only speak for GNAT. GNAT allocates the maximum memory required for an object of a type with a discriminant default value.  
In such cases, GNAT warns of a possible (or a certain) overflow that may (will) be raised at runtime.

Consider:

```ada
procedure Stackblow is

   type Matrix is array (Positive range <>, Positive range <>) of Float;

   type Rip (D : Positive := 5) is record
      Square : Matrix (1..D, 1..D);
   end record;

   Item : Rip; -- tries to allocate Matrix (1..Positive'Last, 1..Positive'Last)

begin
   pragma Assert (False); -- unreachable
end Stackblow;

```

Compiling and running ([Compiler Explorer](https://godbolt.org/z/djTfff8aT)) the above example with GNAT 16.2, I get:

```ada
stackblow.adb:5:09: warning: creation of "Rip" object may raise Storage_Error [enabled by default]
stackblow.adb:9:04: warning: variable "Item" is never read and never assigned [-gnatwu]
stackblow.adb:9:04: warning: Storage_Error will be raised at run time [enabled by default]

$ ./bin/stackblow

raised STORAGE_ERROR : System.Memory.Alloc: object too large

```

To avoid such problems, one should declare a type for the discriminant that ensures no Storage\_Error is possible at runtime, e.g.:

```ada
type Max_Length is Integer range 0 .. 80;
type Maybe_Big_Record (N : Max_Length := 16) is
   record
      My_Name : String (1 .. N);
   end record;

```

This advice is given in [Ada Quality and Style Guide, Discriminated Records](https://ada-lang.io/docs/style-guide/s5/04#discriminated-records)

Of course, one can always consider not using default discriminants or using dynamic storage (e.g. Unbounded\_String).

---

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [November 23, 2026, 8:44am UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979/3 "2026-11-23T08:44:16Z")

</div>

Thanks!

Your answer solves my question.

> [@pmnw](#):
>
> First of all, you forgot to write the default discriminant in your type declaration.

Yes, my bad, and I think also the wording of “[un]constrained” which should be more like “discriminated”. Fixed now.

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [November 23, 2026, 10:26am UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979/4 "2026-11-23T10:26:44Z")

</div>

The Janus/Ada compiler stores the dynamic part on the heap.

Personally, I think that the same program, compiled with different compilers and executed on the same platform, should have identical behavior, regardless of the compiler writers’ implementation decisions.

---

<div class="post-metadata">

**Author:** ![kevlar700](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/kevlar700/32/1118_2.png) [@kevlar700](https://forum.ada-lang.io/u/kevlar700)\
**Post date:** [November 23, 2026, 11:10am UTC](https://forum.ada-lang.io/t/compiler-storage-strategy-for-mutable-discriminated-types/4979/5 "2026-11-23T11:10:45Z")

</div>

> [@JC001](#):
>
> The Janus/Ada compiler stores the dynamic part on the heap.

That wouldn’t work on targets without dynamic storage though such as light runtimes, right?
