comp.lang.ada
 help / color / mirror / Atom feed
* Storage_Error with parameterized records
@ 1998-07-03  0:00 Markus Kuhn
  1998-07-04  0:00 ` Pascal MALAISE
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Markus Kuhn @ 1998-07-03  0:00 UTC (permalink / raw)



May be I have something severely missunderstood, but I have no idea,
why the declaration

   subtype Correlation_Coefficient is Float;

   type CMatrix is
      array (Natural range <>, Natural range <>) of
        Correlation_Coefficient;

   type Correlation_Matrix(Last_Line : Natural := 0) is
      record
	 Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
      end record;

causes the following warnings (and at runtime the promised
Storage_Error):

  warning: Storage_Error will be raised at run-time
  warning: creation of object of this type may raise Storage_Error

I had hoped this code was sufficiently close to John Barnes
Square example in chapter 16 of Programming in Ada 95 and I also
didn't find a restriction in the RM that explained this warning.

And idea?

Markus

-- 
Markus G. Kuhn, Security Group, Computer Lab, Cambridge University, UK
email: mkuhn at acm.org,  home page: <http://www.cl.cam.ac.uk/~mgk25/>




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

* Re: Storage_Error with parameterized records
  1998-07-03  0:00 Storage_Error with parameterized records Markus Kuhn
@ 1998-07-04  0:00 ` Pascal MALAISE
  1998-07-05  0:00 ` david.c.hoos.sr
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Pascal MALAISE @ 1998-07-04  0:00 UTC (permalink / raw)



Let's take the place of the compiler.

>  type CMatrix is
>      array (Natural range <>, Natural range <>) of ...

You want un unconstrained mattrix, fair enough, each dimention between 0
and
integer'last. So far, it's just a type. So far so good.


>   type Correlation_Matrix(Last_Line : Natural := 0) is
>      record
>         Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
>      end record;
It's just a type. No problem so far.
I think you get the compiler warning (and the exception) when you
declare an object of type Correlation_Matrix.

You want a mutant object containing the mattrix. The rule is (often), in
order to allow you to "mute" your object's last_line from 0 to 
integer'last, to allocate the maximum size -> integer'last *
integer'last floats!


So, when you use mutants, use a "reasonable" subtype.


   subtype Correlation_Coefficient is Float;
   subtype Reasonable_Range is Natural range 0 .. 100;

   type CMatrix is
      array (Reasonable_Range range <>, Reasonable_Range range <>) of
        Correlation_Coefficient;

   type Correlation_Matrix(Last_Line : Reasonable_Range := 0) is
      record
         Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
      end record;
Then, when you declare the object, the compiler allocates 100*100
floats.

You can be thankfull to your the compiler for warning you at compile
time.
Not all of the do so  :-)

--
Pascal MALAISE		| E-mail:
22 Avenue de CHOISY	|  (priv) malaise@magic.fr
75013 PARIS		|  (prof) malaise@fr.airsysatm.thomson-csf.com
FRANCE




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

* Re: Storage_Error with parameterized records
  1998-07-03  0:00 Storage_Error with parameterized records Markus Kuhn
  1998-07-04  0:00 ` Pascal MALAISE
@ 1998-07-05  0:00 ` david.c.hoos.sr
  1998-07-08  0:00   ` Robert A Duff
       [not found] ` <dewar.899586342@merv>
  1998-07-10  0:00 ` Samuel Mize
  3 siblings, 1 reply; 8+ messages in thread
From: david.c.hoos.sr @ 1998-07-05  0:00 UTC (permalink / raw)



In article <359D3A0C.13AF4C53@cl.cam.ac.uk>,
  Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> wrote:
>
> May be I have something severely missunderstood, but I have no idea,
> why the declaration
>
>    subtype Correlation_Coefficient is Float;
>
>    type CMatrix is
>       array (Natural range <>, Natural range <>) of
>         Correlation_Coefficient;
>
>    type Correlation_Matrix(Last_Line : Natural := 0) is
>       record
> 	 Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
>       end record;
>
> causes the following warnings (and at runtime the promised
> Storage_Error):
>
>   warning: Storage_Error will be raised at run-time
>   warning: creation of object of this type may raise Storage_Error
>
> I had hoped this code was sufficiently close to John Barnes
> Square example in chapter 16 of Programming in Ada 95 and I also
> didn't find a restriction in the RM that explained this warning.
>
> And idea?
>
> Markus
>
> --
> Markus G. Kuhn, Security Group, Computer Lab, Cambridge University, UK
> email: mkuhn at acm.org,  home page: <http://www.cl.cam.ac.uk/~mgk25/>
>
You didn't supply sufficient information to give a definitive answer.  E.g.,
you did not supply:

  1.  The compiler and platform you are using  2.  The value of the
discriminant with which the record object was declared.

If you consider that the range of type Natural on a 32-bit platform, and a 32-
bit size for type Float could call for just under 2 ** 63 bytes of memory for
an object of type Correlation_Matrix, then the reason for the warning
"creation of object of this type may raise Storage_Error" should be obvious.

The warning "Storage_Error will be raised at run-time" comes as a consequence
of declaring an object which is too large for the implementation.

E.g., with gnat-3.10p1 on Windows NT 4.0 (SP 3), an object of type
Correlation_Matrix declared with a discriminant of 8190 does not raise
Storage_Error at run time.  (The actual limit on the discriminant may depend
on how much other code there is in your program).

This size object represents just under 256 Mb. of memory.  Depending on the
implementation, the limits may be more severe.  E.g., depending on whether the
object was declared statically (e.g., in a package body), on the stack, or
using dynamic memory, the maximum size of an object may differ.

Finally, even though you may restrict the declarations such that the compile-
time warnings are eliminated, bear in mind that there may be run-time
Storage_Errors, if too many instances of legal-sized objects are created.

Hope this helps.

David C. Hoos, Sr.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: Storage_Error with parameterized records
       [not found]   ` <359F50D9.5126DEE1@cl.cam.ac.uk>
@ 1998-07-07  0:00     ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1998-07-07  0:00 UTC (permalink / raw)


Markus said

<<> which just goes to show how this keeps puzzling people. Presumably Markus
> expected an implicit heap implementation here,

Yes, indeed.
>>


Well nothing could show more clearly how it is not at all obvious what the
right choice is. Markus just assumed the heap would be used implicitly, 
whereas some embedded user last week (sorry I forget whom) used this
(implicit heap usage in this situation) as an example of vendors ignoring
users and doing silly things :-)





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

* Re: Storage_Error with parameterized records
  1998-07-05  0:00 ` david.c.hoos.sr
@ 1998-07-08  0:00   ` Robert A Duff
  1998-07-08  0:00     ` Robert Dewar
  0 siblings, 1 reply; 8+ messages in thread
From: Robert A Duff @ 1998-07-08  0:00 UTC (permalink / raw)


david.c.hoos.sr@ada95.com writes:

> In article <359D3A0C.13AF4C53@cl.cam.ac.uk>,
>   Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> wrote:

> >    type Correlation_Matrix(Last_Line : Natural := 0) is
> >       record
> > 	 Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
> >       end record;
> >
> > causes the following warnings (and at runtime the promised
> > Storage_Error):
> >
> >   warning: Storage_Error will be raised at run-time
> >   warning: creation of object of this type may raise Storage_Error

Perhaps a different wording of the message would make it clearer.  The
thing is, if you say "X: Correlation_Matrix(Last_Line => 1_000);" it
will probably *not* raise Storage_Error.  The point is that (1) if you
don't constrain it, it will raise S_E, and (2) there's no point in
giving a default value unless you want to have some unconstrained
objects.  If you leave out ":= 0", I would think the warning would go
away.  But of course you could still get S_E for "Last_Line =>
1_000_000_000" on an object decl.

- Bob
-- 
Change robert to bob to get my real email address.  Sorry.




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

* Re: Storage_Error with parameterized records
  1998-07-08  0:00   ` Robert A Duff
@ 1998-07-08  0:00     ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1998-07-08  0:00 UTC (permalink / raw)


Bob Duff said

<<> >    type Correlation_Matrix(Last_Line : Natural := 0) is
> >       record
> >      Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
> >       end record;
> >
> > causes the following warnings (and at runtime the promised
> > Storage_Error):
> >
> >   warning: Storage_Error will be raised at run-time
> >   warning: creation of object of this type may raise Storage_Error

Perhaps a different wording of the message would make it clearer.  The
thing is, if you say "X: Correlation_Matrix(Last_Line => 1_000);" it
will probably *not* raise Storage_Error.  The point is that (1) if you
don't constrain it, it will raise S_E, and (2) there's no point in
giving a default value unless you want to have some unconstrained
objects.  If you leave out ":= 0", I would think the warning would go
away.  But of course you could still get S_E for "Last_Line =>
1_000_000_000" on an object decl.
>>


Yes, of course the warning goes away if you have no default discriminant,
and further more the warning is only given if you use a type like
Integer (as opposed to Character).

As for the message, Bob is probably one of the few people who really
is accustomed to calling all types in Ada subtypes :-) :-)

When the message says "of this type" it means "of this subtype".

I personally find the change in terminology in the Ada 95 RM here
confusing, and certainly people do not use it in common usage. Oh well.

Certainly we could use subtype in this case I suppose! Or put in the
word unconstrained.





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

* Re: Storage_Error with parameterized records
  1998-07-03  0:00 Storage_Error with parameterized records Markus Kuhn
                   ` (2 preceding siblings ...)
       [not found] ` <dewar.899586342@merv>
@ 1998-07-10  0:00 ` Samuel Mize
  1998-07-13  0:00   ` Robert Dewar
  3 siblings, 1 reply; 8+ messages in thread
From: Samuel Mize @ 1998-07-10  0:00 UTC (permalink / raw)


If you create an unconstrained instance of this type, your compiler
allocates enough space to hold the largest example: an array of
Natural*Natural elements.  This is large.

If you only create constrained instances, you should be OK.  If you
need to be able to change the constraint, you need to determine the
largest constraint you may need, and use a subtype constrained by
that maximum value (instead of Natural) for the constraint.  Depending
on how smart the compiler is, you may need to use that subtype for
the indices of CMatrix too.

Best,
Sam Mize

In article <359D3A0C.13AF4C53@cl.cam.ac.uk>,
Markus Kuhn  <Markus.Kuhn@cl.cam.ac.uk> wrote:
>May be I have something severely missunderstood, but I have no idea,
>why the declaration
>
>   subtype Correlation_Coefficient is Float;
>
>   type CMatrix is
>      array (Natural range <>, Natural range <>) of
>        Correlation_Coefficient;
>
>   type Correlation_Matrix(Last_Line : Natural := 0) is
>      record
>	 Mat : CMatrix(0 .. Last_Line, 0 .. Last_Line);
>      end record;
>
>causes the following warnings (and at runtime the promised
>Storage_Error):
>
>  warning: Storage_Error will be raised at run-time
>  warning: creation of object of this type may raise Storage_Error


-- 
Samuel Mize -- smize@imagin.net (home email) -- Team Ada
Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam




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

* Re: Storage_Error with parameterized records
  1998-07-10  0:00 ` Samuel Mize
@ 1998-07-13  0:00   ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1998-07-13  0:00 UTC (permalink / raw)


Samuel says

<<If you only create constrained instances, you should be OK.  If you
need to be able to change the constraint, you need to determine the
largest constraint you may need, and use a subtype constrained by
that maximum value (instead of Natural) for the constraint.  Depending
on how smart the compiler is, you may need to use that subtype for
the indices of CMatrix too.
>>


But if you only create constrained instances, then it is meaningless,
and misleading, to provide default values for the discriminants. The
ONLY purpose for providing default values is to enable you to
create unconstrained instances!





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

end of thread, other threads:[~1998-07-13  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-07-03  0:00 Storage_Error with parameterized records Markus Kuhn
1998-07-04  0:00 ` Pascal MALAISE
1998-07-05  0:00 ` david.c.hoos.sr
1998-07-08  0:00   ` Robert A Duff
1998-07-08  0:00     ` Robert Dewar
     [not found] ` <dewar.899586342@merv>
     [not found]   ` <359F50D9.5126DEE1@cl.cam.ac.uk>
1998-07-07  0:00     ` Robert Dewar
1998-07-10  0:00 ` Samuel Mize
1998-07-13  0:00   ` Robert Dewar

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