comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de>
Subject: Re: dynamic array as return value?
Date: Fri, 15 Oct 2004 16:36:09 +0000 (UTC)
Date: 2004-10-15T16:36:09+00:00	[thread overview]
Message-ID: <ckou9p$svv$1@a1-hrz.uni-duisburg.de> (raw)
In-Reply-To: cknpmb$hk8$1@netnews.hinet.net

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



  reply	other threads:[~2004-10-15 16:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2004-10-19  5:35 ` bubble
replies disabled

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