comp.lang.ada
 help / color / mirror / Atom feed
* Re: dynamic array as return value?
       [not found] <cklea4$9eb$1@netnews.hinet.net>
@ 2004-10-14 17:24 ` Nick Roberts
  2004-10-14 17:28 ` Georg Bauhaus
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Nick Roberts @ 2004-10-14 17:24 UTC (permalink / raw)


bubble wrote:

> In Ada the bounds of an array need not be fixed at compile-time, as they can
> be specified by an object whose
> value is not fixed until run-time. Such an array is known as a dynamic
> array. However, once elaborated, the
> bounds of the dynamic array cannot be changed.

I think this could be put better, to be honest. In Ada, the bounds of an
array /value/ can be dynamic, but the bounds of an /object/ cannot change
dynamically. An 'object' is a variable, including a component of an array
or record value.

Since a function returns an object, the bounds of an array object
returned by a function cannot be changed dynamically. However, the
expression which is evaluated to construct the object which is returned
can have dynamically calculated bounds.

> Unlike many other languages, Ada allows a dynamic array to be returned
> as the result of a function. For example, a function Reverse_String
> can be written which reverses the characters passed to it.
> 
> An implementation of the function Reverse_String is as follows:
> 
> function Reverse_String( Str:in String ) return String is
>     Res : String( Str'Range ); --Dynamic bounds
> begin
>     for I in Str'Range loop
>         Res( Str'First+Str'Last-I ) := Str( I );
>     end loop;
>     return Res;
> end Reverse_String;

When this function executes the 'return' statement, the return expression
(Res) is evaluated, to construct an object (of the type String), and this
object is returned. The bounds are dynamically calculated, but once
calculated they cannot be changed.

Every time Reverse_String is called, the bounds of Res are evaluated
(dynamically), and Res then has these bounds, for the rest of its
lifetime -- which is the duration of the execution of the function -- and
once they are set, they cannot be changed during this lifetime.

So the bounds of the object Res cannot be changed dynamically (they can
be initially /set/ dynamically, but never /changed/ dynamically). Since
the return value is simply Res, this means that the bounds of the return
value are calculated dynamically (but then cannot be changed).

This is different to the strings of many other languages (e.g. BASIC),
in which you can dynamically change the upper bound of a variable at any
time (causing characters to be appended or truncated).

However, Ada supplies the library types
Ada.Strings.Bounded.Generic_Bounded_Strings.Bounded_String and
Ada.Strings.Unbounded.Unbounded_String. These strings types do allow
the upper bound of variables to be changed dynamically. It's just a
pity they have such long names!

> If ada allow dynamic array can be a return value,it mean bounds array
> can be return value?

Yes, the bounds -- both upper and lower -- of Str are passed into the
function  every time it is called, and both bounds of the return result
are also passed out. (It so happens, in this function, that these bounds
are the same.)

The fact that the bounds of Str are passed in is how Str'First, Str'Last,
and Str'Range can be used in the function.

I hope I've made things clear. Please ask if you have any more questions!

-- 
Nick Roberts



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

* Re: dynamic array as return value?
       [not found] <cklea4$9eb$1@netnews.hinet.net>
  2004-10-14 17:24 ` dynamic array as return value? Nick Roberts
@ 2004-10-14 17:28 ` Georg Bauhaus
  2004-10-15  3:04 ` Steve
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2004-10-14 17:28 UTC (permalink / raw)


bubble <bubble@bubble.d2g.com> wrote:
: 
: I have a question.
: if ada allow dynamic array can be a return value,it mean bounds array can be
: return value?
: if can't,why?

You mean, can a function return an array with fixed bounds?
As for example in

  subtype String_80 is String(1.. 80);

  function ... return String_80;





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

* Re: dynamic array as return value?
       [not found] <cklea4$9eb$1@netnews.hinet.net>
  2004-10-14 17:24 ` dynamic array as return value? Nick Roberts
  2004-10-14 17:28 ` Georg Bauhaus
@ 2004-10-15  3:04 ` Steve
  2004-10-15  6:02 ` bubble
  2004-10-19  5:35 ` bubble
  4 siblings, 0 replies; 6+ messages in thread
From: Steve @ 2004-10-15  3:04 UTC (permalink / raw)


"bubble" <bubble@bubble.d2g.com> wrote in message
news:cklea4$9eb$1@netnews.hinet.net...
[snip]
> I have a question.
> if ada allow dynamic array can be a return value,it mean bounds array can
be
> return value?
> if can't,why?
>

Please try to re-phrase you question.  It is very hard to understand exactly
what you are asking.  There are many helpful people on this group that will
have a much easier time answering your question if they can understand it.

Steve
(The Duck)





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

* Re: dynamic array as return value?
       [not found] <cklea4$9eb$1@netnews.hinet.net>
                   ` (2 preceding siblings ...)
  2004-10-15  3:04 ` Steve
@ 2004-10-15  6:02 ` bubble
  2004-10-15 16:36   ` Georg Bauhaus
  2004-10-19  5:35 ` bubble
  4 siblings, 1 reply; 6+ messages in thread
From: bubble @ 2004-10-15  6:02 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2011 bytes --]



subtype String_80 is String(1.. 80);



function makeSpaceString return String is

      spaceStr:String_80 := (others => ' ');

begin

      return spaceStr;  --can I do it?

end makeSpaceString;





function makeSpaceString(length:Natural) return String is

      spaceStr :string(1..length) := (others => ' ');

begin

      return spaceStr;   --- ???

end makeSpaceString;





Hi dear:

Thank your answer.





my question is about memory allocation in ada95.



I remember,local variable memory allocate should use stack not heap memory.



In above 2 functions.



the variable spaceStr are local variable in function,right?



after execute end of function,data in stack memory should free to system.

it mean I return a data in stack and it is free,it will occure illegal
access?

I don't understand why "dynamic array" can be return?

"bubble" <bubble@bubble.d2g.com> �b�l�� news:cklea4$9eb$1@netnews.hinet.net
�����g...
> according to a book, " Object-oriented Software in Ada 95 Second Edition"
>
> section 8.9 Dynamic Array
>
>
> In Ada the bounds of an array need not be fixed at compile-time, as they
can
> be specified by an object whose
> value is not fixed until run-time. Such an array is known as a dynamic
> array. However, once elaborated, the
> bounds of the dynamic array cannot be changed. Unlike many other
languages,
> Ada allows a dynamic array to be
> returned as the result of a function. For example, a function
Reverse_String
> can be written which reverses
> the characters passed to it.
>
> An implementation of the function Reverse_String is as follows:
>
> function Reverse_String( Str:in String ) return String is
>     Res : String( Str'Range ); --Dynamic bounds
> begin
>     for I in Str'Range loop
>         Res( Str'First+Str'Last-I ) := Str( I );
>     end loop;
>     return Res;
> end Reverse_String;
>
>
> I have a question.
> if ada allow dynamic array can be a return value,it mean bounds array can
be
> return value?
> if can't,why?
>
>





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

* Re: dynamic array as return value?
  2004-10-15  6:02 ` bubble
@ 2004-10-15 16:36   ` Georg Bauhaus
  0 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2004-10-15 16:36 UTC (permalink / raw)


bubble <bubble@bubble.d2g.com> wrote:
:      return spaceStr;  --can I do it?
yes

: function makeSpaceString(length:Natural) return String is
: 
:      spaceStr :string(1..length) := (others => ' ');
: 
: begin
: 
:      return spaceStr;   --- ???

yes
 
: my question is about memory allocation in ada95.
: 
: I remember,local variable memory allocate should use stack not heap memory.
: In above 2 functions.
: the variable spaceStr are local variable in function,right?

yes

: after execute end of function,data in stack memory should free to system.

  before this happens, the returned value is used in an expression, 
  for example,

    spaces := makeSpaceString(20);  -- copies result to spaces

  or
 
    if my_spaces = makeSpaceString(15) then
      ...

"=" is a function of two arguments, the above could also be written
as

    if "="(my_spaces, makeSpaceString(15)) then
      ...

I suggest that you look at what the compiler produces for
such a call.

: it mean I return a data in stack and it is free,it will occure illegal
: access?

In Ada case you can be pretty sure that returning will work
no matter what the type is, or else the compiler will tell you
you can't do this, for example if you try to return a pointer to
a local scalar variable. But

   return some_string;

is not necessarily returning a pointer.


-- Georg



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

* Re: dynamic array as return value?
       [not found] <cklea4$9eb$1@netnews.hinet.net>
                   ` (3 preceding siblings ...)
  2004-10-15  6:02 ` bubble
@ 2004-10-19  5:35 ` bubble
  4 siblings, 0 replies; 6+ messages in thread
From: bubble @ 2004-10-19  5:35 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1206 bytes --]

thank you,all
your answer is very useful..
"bubble" <bubble@bubble.d2g.com> �b�l�� news:cklea4$9eb$1@netnews.hinet.net
�����g...
> according to a book, " Object-oriented Software in Ada 95 Second Edition"
>
> section 8.9 Dynamic Array
>
>
> In Ada the bounds of an array need not be fixed at compile-time, as they
can
> be specified by an object whose
> value is not fixed until run-time. Such an array is known as a dynamic
> array. However, once elaborated, the
> bounds of the dynamic array cannot be changed. Unlike many other
languages,
> Ada allows a dynamic array to be
> returned as the result of a function. For example, a function
Reverse_String
> can be written which reverses
> the characters passed to it.
>
> An implementation of the function Reverse_String is as follows:
>
> function Reverse_String( Str:in String ) return String is
>     Res : String( Str'Range ); --Dynamic bounds
> begin
>     for I in Str'Range loop
>         Res( Str'First+Str'Last-I ) := Str( I );
>     end loop;
>     return Res;
> end Reverse_String;
>
>
> I have a question.
> if ada allow dynamic array can be a return value,it mean bounds array can
be
> return value?
> if can't,why?
>
>





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

end of thread, other threads:[~2004-10-19  5:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cklea4$9eb$1@netnews.hinet.net>
2004-10-14 17:24 ` dynamic array as return value? Nick Roberts
2004-10-14 17:28 ` Georg Bauhaus
2004-10-15  3:04 ` Steve
2004-10-15  6:02 ` bubble
2004-10-15 16:36   ` Georg Bauhaus
2004-10-19  5:35 ` bubble

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