comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: How much storage requires this record?
Date: Tue, 19 Aug 2014 14:14:51 -0400
Date: 2014-08-19T14:14:51-04:00	[thread overview]
Message-ID: <wcciolofisk.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: lt03dc$4n2$1@speranza.aioe.org

Victor Porton <porton@narod.ru> writes:

> How much storage requires the following record? I am afraid that in some 
> compilers each string would require about Natural'Last bytes (way too much).

No, there are no Ada compilers that do that.  The discriminants do not
have defaults, which means every object of type Filename_And_Fragment is
constrained, so they can't change size, so the compiler can (and will)
allocate just the necessary amount.  This size is calculated when the
object is created (or at compile time, if possible).

But if you add a default value:

    type Filename_And_Fragment (Filename_Length, Fragment_Length: Natural := 0) is

then some objects can be unconstrained, and some compilers will allocate
the max size for those.  GNAT does that, and also warns about things
like the above.  Other compilers use a deallocate/reallocate strategy.

Tying "all objects constrained" to "discrims do not have defaults" was a
questionable design decision.  It confuses programmers.

To add to the confusion, if you add "limited" to the above, then you're
back in the "can't change size" situation.

>    type Filename_And_Fragment (Filename_Length, Fragment_Length: Natural) is
>       record
>          Filename: String(1..Filename_Length);
>          Fragment: String(1..Fragment_Length);
>       end record;
>
> If the above is wrong (from memory positions), what is the right way to do 
> it?

Nothing wrong with the above.

- Bob


      reply	other threads:[~2014-08-19 18:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-19 18:01 How much storage requires this record? Victor Porton
2014-08-19 18:14 ` Robert A Duff [this message]
replies disabled

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