comp.lang.ada
 help / color / mirror / Atom feed
* How much storage requires this record?
@ 2014-08-19 18:01 Victor Porton
  2014-08-19 18:14 ` Robert A Duff
  0 siblings, 1 reply; 2+ messages in thread
From: Victor Porton @ 2014-08-19 18:01 UTC (permalink / raw)


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).

   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?

-- 
Victor Porton - http://portonvictor.org


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How much storage requires this record?
  2014-08-19 18:01 How much storage requires this record? Victor Porton
@ 2014-08-19 18:14 ` Robert A Duff
  0 siblings, 0 replies; 2+ messages in thread
From: Robert A Duff @ 2014-08-19 18:14 UTC (permalink / raw)


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-19 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-19 18:01 How much storage requires this record? Victor Porton
2014-08-19 18:14 ` Robert A Duff

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