comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Legit Warnings or not
Date: Wed, 20 Jul 2011 19:16:55 -0400
Date: 2011-07-20T19:16:55-04:00	[thread overview]
Message-ID: <wccy5zslezc.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 531193e0-3305-4292-9ed8-0176226c1d00@x12g2000yql.googlegroups.com

Anh Vo <anhvofrcaus@gmail.com> writes:

> package Warnings_Legit is
>
>    type Warned_Person (Size : Positive := 10) is -- Warnings issued
> here
>       record
>          Name : String (1 .. Size);
>       end record;
>
>    type Acceptable_Person (Size : Positive) is
>       record
>          Name : String (1 .. Size);
>       end record;
>
> end Warnings_Legit;
>
> The code segment above triggers a warnings message 'creation of
> "Warned_Person" object may raise Storage_Error' at line 3 as marked.
> However, no warnings is issued at line 8. The difference between them
> is default discriminant. I am using GNAT-GPL-2011.

There's a confusing rule in Ada:  If it has defaults, there can be
unconstrained objects of that type (and also constrained ones).
If it doesn't have defaults, then all objects must be constrained.

So if you say "X : Warned_Person;", GNAT will allocate space for
billions of characters (not just 10) because you might assign
a bigger one (like "X := (Size => 1_000_000, Name => Something)").

> Is this warnings legitimate? If yes, why line 8 is OK. Otherwise,
> should it be a bug?

It's not a bug.  An object of type Warned_Person might need huge amounts
of memory, so Storage_Error might well be raised.  It's illegal to say
"X : Acceptable_Person;", so that one doesn't get a warning.

If you want unconstrained objects, use a reasonably-small size, like:

   subtype Name_Length is Natural range 0..1000;

   type Not_Warned_Person (Size : Name_Length := 0) is
      record
         Name : String (1 .. Size);
      end record;

Unconstrained objects of type Not_Warned_Person will then require space
for 1000 characters.

On the other hand, if you don't want unconstrained objects, don't give a
default (as in your Acceptable_Person).

- Bob



  reply	other threads:[~2011-07-20 23:16 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-20 22:30 Legit Warnings or not Anh Vo
2011-07-20 23:16 ` Robert A Duff [this message]
2011-07-21 18:43   ` Anh Vo
2011-07-23  0:26   ` Randy Brukardt
2011-07-23 14:26     ` Robert A Duff
2011-07-21  2:37 ` Jeffrey Carter
2011-07-21  9:50   ` Brian Drummond
2011-07-21 14:39     ` Dmitry A. Kazakov
2011-07-23  0:36       ` Randy Brukardt
2011-07-23  9:03         ` Dmitry A. Kazakov
2011-07-23 11:07           ` Simon Wright
2011-07-23 11:21             ` Dmitry A. Kazakov
2011-07-26 21:25           ` Randy Brukardt
2011-07-27  7:45             ` Dmitry A. Kazakov
2011-07-28  0:37               ` Randy Brukardt
2011-07-28  9:22                 ` Dmitry A. Kazakov
2011-07-28 14:22                   ` Robert A Duff
2011-07-28 14:41                     ` Dmitry A. Kazakov
2011-07-28 15:10                       ` Robert A Duff
2011-07-28 17:05                         ` Dmitry A. Kazakov
2011-07-28 23:32                           ` Randy Brukardt
2011-07-28 23:48                   ` Randy Brukardt
2011-07-29  6:57                     ` Simon Wright
2011-07-29 18:56                       ` Jeffrey Carter
2011-07-30  0:13                       ` Randy Brukardt
2011-07-29  7:41                     ` Dmitry A. Kazakov
2011-07-30  0:17                       ` Randy Brukardt
2011-07-30  8:27                         ` Dmitry A. Kazakov
2011-08-01 22:12                           ` Randy Brukardt
2011-08-02 10:01                             ` Dmitry A. Kazakov
2011-08-02 21:30                               ` Randy Brukardt
2011-08-03  9:01                                 ` Dmitry A. Kazakov
2011-08-03 20:35                                   ` Randy Brukardt
2011-08-04  8:11                                     ` Dmitry A. Kazakov
2011-08-05 23:57                                       ` Randy Brukardt
2011-08-06  8:23                                         ` Dmitry A. Kazakov
2011-08-08 21:30                                           ` Randy Brukardt
2011-07-23 14:32         ` Robert A Duff
2011-07-26 21:32           ` Randy Brukardt
2011-07-21 15:28     ` Adam Beneschan
2011-07-21 15:41       ` Robert A Duff
2011-07-21 20:12         ` Adam Beneschan
2011-07-23  0:31           ` Randy Brukardt
2011-07-21 17:40     ` Jeffrey Carter
2011-07-21 21:30       ` Brian Drummond
2011-07-21 21:54         ` Adam Beneschan
2011-07-22 10:16           ` Brian Drummond
2011-07-22 14:54             ` Adam Beneschan
2011-07-23  0:46             ` Randy Brukardt
2011-07-23  0:42         ` Randy Brukardt
2011-07-23  9:20           ` Niklas Holsti
2011-08-04  5:56             ` David Thompson
2011-07-23  9:24           ` Dmitry A. Kazakov
2011-07-23 12:36           ` Brian Drummond
2011-07-21 15:13   ` Robert A Duff
2011-07-23  0:52     ` Randy Brukardt
2011-07-23 14:48       ` Robert A Duff
2011-07-21 14:17 ` anon
replies disabled

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