return value as an allocatable array 
Author Message
 return value as an allocatable array

Let's say I have a function where the return value is an allocatable
array.

! I am just using this as an example.
! My actual problem is a bit different.
function dot_square(elem_no, A) return (Asqr)
  integer, intent(in) :: elem_no
  real :: A(1:elem_no, 1:elem_no)

  real, allocatable :: Asqr(:,:)

  allocate(Asqr(elem_no, elem_no)
  Asqr = A * A
end function dot_square

It creates an array (for the return value) but never deallocates it.
Will this create a memory leak if I keep repeatedly calling the
function (say in a do loop)?

I want to create the return value as an allocatable array because
sometimes elem_no could be large.

I want to use the function interface instead of calling a subroutine
because I would like to do

Apow4 = dot_square(dot_square(A))

If it does create a memory leak, What is the best way to approach this
problem?

If it helps, I am using gfortran 4.3.2, Fortran 90, Debian Etch.

thanks
raju



Sat, 16 Apr 2011 23:44:59 GMT  
 return value as an allocatable array

Quote:

> Let's say I have a function where the return value is an allocatable
> array.
...
> It creates an array (for the return value) but never deallocates it.
> Will this create a memory leak if I keep repeatedly calling the
> function (say in a do loop)?

See the many, many prior posts where it is explained that allocatabes in
standard f95 do not have memory leaks. Ever. Since there are no cases
that leak memory, this is not one of the non-existant cases.

The "standard" above allows me to justify the "ever" because the onbly
cases that leak memory involve either nonstandard behavior in the
compiler or in the code. The only 2 exceptions are, in essence,...

1. Compiler bugs.

2. Program bugs.

Not much more to say about compiler bugs. They can exist. Program bugs
only half count as exceptions anyway, because generally they are most
likely to be program bugs in something else that might show syptoms in
allocatables. For example, if you trash memory by pointer misuse, by
exceeding array bounds, or by argument mismatches, the symptoms could
show up in allocatables... or in pretty much anything else.

Not related to memory leaks, but do note that your example of

Quote:
> Apow4 = dot_square(dot_square(A))

will requre that Apow4 be allocated prior to this statement unless you
have an f2003 compiler, which you don't.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



Sun, 17 Apr 2011 00:08:11 GMT  
 return value as an allocatable array

Quote:

> > Let's say I have a function where the return value is an allocatable
> > array.
> ...
> > It creates an array (for the return value) but never deallocates it.
> > Will this create a memory leak if I keep repeatedly calling the
> > function (say in a do loop)?

> See the many, many prior posts where it is explained that allocatabes in
> standard f95 do not have memory leaks. Ever. Since there are no cases
> that leak memory, this is not one of the non-existant cases.

Did you notice that F90 is in that list?

    > >If it helps, I am using gfortran 4.3.2, Fortran 90, Debian Etch.

It's a legitimate query, as there were leaks in some F90 compilers.

Quote:
> The "standard" above allows me to justify the "ever" because the onbly
> cases that leak memory involve either nonstandard behavior in the
> compiler or in the code. The only 2 exceptions are, in essence,...

> 1. Compiler bugs.

> 2. Program bugs.

> Not much more to say about compiler bugs. They can exist. Program bugs
> only half count as exceptions anyway, because generally they are most
> likely to be program bugs in something else that might show syptoms in
> allocatables. For example, if you trash memory by pointer misuse, by
> exceeding array bounds, or by argument mismatches, the symptoms could
> show up in allocatables... or in pretty much anything else.

> Not related to memory leaks, but do note that your example of

> > Apow4 = dot_square(dot_square(A))

> will requre that Apow4 be allocated prior to this statement unless you
> have an f2003 compiler, which you don't.

There's no requirement that Apow4 has to be allocatable.
It could, in point of fact, be an ordinary dynamic array
having the same extents as A.


Fri, 29 Apr 2011 05:38:16 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Allocatable array as return value in function

2. java array return value not array in tcl

3. Possible to return an allocatable array?

4. F95: Possible to return an allocatable array?

5. How can I return an allocatable array?

6. f90 function returns structure with allocatable arrays

7. debugger: examine values of allocatable array (OVMS)

8. allocatable vs non-allocatable arrays

9. Allocatable array of allocatable defined types?

10. Copying one array value into subsequent array values

11. Returning Array Values?

12. Return value that is an access to array element

 

 
Powered by phpBB® Forum Software