comp.lang.ada
 help / color / mirror / Atom feed
* Re: What should this do?
@ 1991-07-02 17:09 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre
  0 siblings, 0 replies; 10+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre @ 1991-07-02 17:09 UTC (permalink / raw)


You might use the following program to get a glimpse of what your Ada system
is doing with mutable records. It's not completely definitive but gives a 
good clue. Sorry for the global variables - this is one off quick and
dirty.

PS to Mike Feldman - Re your question on what Alsys does in this case.
This was run on Alsys Ada on AIX/370, IBM 3090/400. It appears that they
allocate maximum space (20 bytes) and have the string bob about in it.
Good for speed, bad for space utilization


-- Check Alsys techniques for mutable records.
with text_io; use text_io;
with system;  -- Alsys version has a procedure "image" that makes
              -- addresses into strings.
procedure main is 
   subtype index is integer range 0..20;
   type rec(len : index := 0) is record
      data : string(1..len);
   end record;
   S : rec;
procedure print is
begin
   put_line("S's address is " & system.image(S'address) & " hex");
   put_line("For S.len =" & integer'image(S.len) & ',' & 
           integer'image(S'size/8) & " bytes are used."); new_line;
end print;
begin
   S := (0, "");
   print;

   S := (10,"0123456789");
   print;

   S := (20,"01234567890123456789");
   print;

   S := (0,"");
   print;
end main;

Results of execution:

S's address is 0024BAFC hex
For S.len = 0, 7 bytes are used.

S's address is 0024BAFC hex
For S.len = 10, 17 bytes are used.

S's address is 0024BAFC hex
For S.len = 20, 27 bytes are used.

S's address is 0024BAFC hex
For S.len = 0, 7 bytes are used.


Neil Webre
Cal Poly, San Luis Obispo, CA
Department of Computer Science.

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: What should this do?
@ 1991-07-02 13:06 Fred Stluka
  0 siblings, 0 replies; 10+ messages in thread
From: Fred Stluka @ 1991-07-02 13:06 UTC (permalink / raw)


In article <1991Jul01.152134.16085@afit.af.mil> dlindsle@afit.af.mil (David T. 
Lindsley) writes:
>
>   subtype Index is natural; -- range 0..100;
>   type Dyn_String (Size : Index := 0) is record
>     Data : string (1..Size);
>   end record;
> 
>   S: Dyn_String;
> 
> But this still leaves me with a question.  Why does this work with a
> constrained subtype, but not with an unconstrained one?  Either results
> in the elaboration of a null array -- why is this a problem with an
> unconstrained subtype?  (Especially since NATURAL is a discrete (sub)type
> anyway...

This is a problem for many compilers for exactly the reasons 
already mentioned in previous followups.

When a default value is supplied for the discriminant of 
the record, and then an *unconstrained* object is declared,
the value of the discriminant for that object can be changed
later.

Therefore, the record is probably being allocated with enough
space to contain an array of the maximum size allowed, despite
that fact that its *initial* size is small.  When the Index type
is allowed to get as large as natural'last, this is a pretty 
large array.

Two ways to fix this:

     1)  Constrain Index to a smaller range as you did, 
         restricting the potential size of Dyn_String,
         so that less space gets allocated.

     2)  Declare S with a constraint:
               S : Dyn_String (100);
         but this is kind of pointless, because then S is
         required to *always* be exactly 100 chars long and
         is no longer a "dynamic" string.

Some if this was explained better and in more detail in previous
followups to the original question.  I suggest that you go back 
and re-read them.

--Fred
-- 
Fred Stluka                               Internet: stluka@software.org
Software Productivity Consortium          UUNet:    ...!uunet!software!stluka
2214 Rock Hill Rd, Herndon VA 22070 USA   Voice:    (703)742-7236

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: What should this do?
@ 1991-07-01 15:21 voder!wlbr!lonex.radc.af.mil!blackbird.afit.af.mil!dlindsle
  0 siblings, 0 replies; 10+ messages in thread
From: voder!wlbr!lonex.radc.af.mil!blackbird.afit.af.mil!dlindsle @ 1991-07-01 15:21 UTC (permalink / raw)


rharwood@east.pima.edu writes:

>In article <1991Jun28.193513.14271@afit.af.mil>, dlindsle@afit.af.mil (David T
.
>Lindsley) writes:


>procedure test_100 is

>             subtype INDEX is natural range 0..100;
>             type DYN_STRING (SIZE : INDEX := 0) is private; 

>           private
>                 type DYN_STRING (size : index := 0) is
>                    record
>                        DATA : string (1..size);
>                    end record;

And this version does compile, even here.  Sorry about that, folks.  Our
AC is broken (it's 85F/30C-plus in here), and I'd been messing with that
package all day.
What I couldn't get to compile/run was:


  subtype Index is natural; -- range 0..100;
  type Dyn_String (Size : Index := 0) is record
    Data : string (1..Size);
  end record;

  S: Dyn_String;


But this still leaves me with a question.  Why does this work with a
constrained subtype, but not with an unconstrained one?  Either results
in the elaboration of a null array -- why is this a problem with an
unconstrained subtype?  (Especially since NATURAL is a discrete (sub)type
anyway...

-- 
Dave Lindsley	#24601#			OPINIONS.  MINE.  (Nobody tells me
dlindsle@blackbird.afit.af.mil		  anything anyway, so I can't possibly 
    ?? lamroN eb yhW ??			  be anybody's mouthpiece...)

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: What should this do?
@ 1991-07-01 12:45 CBW Consulting
  0 siblings, 0 replies; 10+ messages in thread
From: CBW Consulting @ 1991-07-01 12:45 UTC (permalink / raw)


There is nothing wrong with declaring an array to have a null range
(lower bound > upper bound) and this should not raise an exception on
any validated compiler.

You will however get an exception if you try to access a null array.
Now since the dynamic string type is declared as a private type, the
package that declares the private type should have all the operations
you need to manipulate strings.  I have used dynamic strings of this
type on several different compiler implementations without error.

Chuck Williams
CBW Consulting

^ permalink raw reply	[flat|nested] 10+ messages in thread
* What should this do?
@ 1991-06-28 19:35 David T. Lindsley
  1991-06-28 21:40 ` Howard Turner
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: David T. Lindsley @ 1991-06-28 19:35 UTC (permalink / raw)



I have a question about dynamic strings.  I was looking at a package
on simtel20 that did the following:

             subtype INDEX is natural range 0..100;
             type DYN_STRING (SIZE : INDEX := 0) is private; 

--           private
--                 type DYN_STRING (size : index := 0) is
--                    record
--                        DATA : string (1..size);
--                    end record;

the second declaration had a note to the effect of "uncomment and use
this if you've got a VALIDATED compiler".

Now as far as I can tell, during elaboration, a declaration of the form

	S : DYN_STRING;

will result in the attempt to allocate an array constrained to (1..0),
which should raise an exception.  At least, that's what it does under
VAX Ada, but not on Verdix.  (Both generate warnings.)

It seems to me this has to be a bug in one of the compilers.  My question
is: whose?  Should this, or should it not, raise an exception?  (The
LRM references weren't any help.)

-- 
Dave Lindsley	#24601#			OPINIONS.  MINE.  (Nobody tells me
dlindsle@blackbird.afit.af.mil		  anything anyway, so I can't possibly 
    ?? lamroN eb yhW ??			  be anybody's mouthpiece...)

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

end of thread, other threads:[~1991-07-02 17:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-07-02 17:09 What should this do? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre
  -- strict thread matches above, loose matches on Subject: below --
1991-07-02 13:06 Fred Stluka
1991-07-01 15:21 voder!wlbr!lonex.radc.af.mil!blackbird.afit.af.mil!dlindsle
1991-07-01 12:45 CBW Consulting
1991-06-28 19:35 David T. Lindsley
1991-06-28 21:40 ` Howard Turner
1991-06-29  0:31 ` Jim Showalter
1991-06-29 16:44   ` Michael Feldman
1991-06-29 21:22     ` Jim Showalter
1991-06-29  5:52 ` rharwood

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