public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andre Vehreschild <vehre@gmx.de>
To: Steve Kargl <sgk@troutmask.apl.washington.edu>
Cc: Mikael Morin <mikael.morin@sfr.fr>,
	GCC-Patches-ML <gcc-patches@gcc.gnu.org>,
	GCC-Fortran-ML <fortran@gcc.gnu.org>,
	Paul Richard Thomas <paul.richard.thomas@gmail.com>
Subject: Re: [RFC, Fortran, (pr66775)] Allocatable function result
Date: Sat, 11 Jul 2015 10:37:00 -0000	[thread overview]
Message-ID: <20150711123648.1ac46e2a@vepi2> (raw)
In-Reply-To: <20150710185713.GA92724@troutmask.apl.washington.edu>

[-- Attachment #1: Type: text/plain, Size: 1755 bytes --]

Hi,

<snip>
>    "On completion of execution of the function, the value returned
>     is that of its function result. ... If the function result is
>     not a pointer, its value shall be defined by the function."

Now we can argue whether the "shall be defined" is to be interpreted as "has to
be" or as "might be". For me - being a non-native English speaker - that "shall"
is not an obligation but should be interpreted as "commonly the function result
is to be defined, but there can be exceptions". Now I am curious about how
native English speakers understand that standard statement.

My argument on returning an unallocated object is not to support programatical
errors, but rather for circumstances, where one tries to allocate a resource
and on failure nothing is returned. Think about a function like
try_to_acquire_resource(), when the resource could be reserved (like, for
example, a co-processor), then its (private) structure is returned
(implementing the OO concept of information hiding). Furthermore one could
think about using concepts like memory pools, where a memory fragment is
returned by a function as long as the capacity of the pool is no exhausted.
This of course is more difficult in Fortran as is lacks the ways of doing
C-style pointer arithmetic. This is just an example. So please don't nail me on
this one. I just wanted to give you my idea, why I think returning an
unallocated "object" should be legal for an allocatable function result. (Note,
the result is called allocatable, not allocated function result :-). Meaning,
that it can be allocated, but does not have to be.)

When someone has other compilers available the test program is attached.

Regards,
	Andre
-- 
Andre Vehreschild * Email: vehre ad gmx dot de 

[-- Attachment #2: test_pr66775.f03 --]
[-- Type: application/octet-stream, Size: 1363 bytes --]

! { dg-do run }

module test_alloc_func_mod
  implicit none

  type t
    integer :: i = 5
  end type

contains

  type(t) function t_init()
    allocatable :: t_init
  end function

  type(t) function t_init2()
    allocatable :: t_init2
    allocate(t_init2)
  end function

  integer function i_init()
    allocatable :: i_init
  end function

  integer function i_init2()
    allocatable :: i_init2
    allocate(i_init2)
  end function

end module test_alloc_func_mod

program test_alloc_func
  use test_alloc_func_mod

  type(t), allocatable :: temp
  type(t), allocatable :: vec(:)
  integer, allocatable :: i

  temp = t_init()
  if (allocated (temp)) call abort()

  temp = t_init2()
  if (.not. allocated (temp)) call abort()
  if (temp%i /= 5) call abort()
  deallocate(temp)

! The next two lines create a memory corruption/segfault, because the result
! of i_init() is not allocated but dereferenced.
!  i = i_init()
!  if (allocated (i)) call abort()

  i = i_init2()
  if (.not. allocated(i)) call abort()
  deallocate(i)

  allocate (vec(5))
  vec = t_init2()
  if (.not. allocated(vec)) call abort()
  if (any(vec%i /= [5,5,5,5,5])) call abort()
  deallocate (vec)

  allocate (vec(5))
  vec(:) = t_init2() ! Note the colon op.
  if (.not. allocated(vec)) call abort()
  if (any(vec%i /= [5,5,5,5,5])) call abort()
  deallocate (vec)

end program

  reply	other threads:[~2015-07-11 10:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-09 10:25 Andre Vehreschild
2015-07-09 17:50 ` Steve Kargl
2015-07-09 18:59   ` Andre Vehreschild
2015-07-09 19:41     ` Steve Kargl
2015-07-10  9:44       ` Andre Vehreschild
2015-07-10 13:41         ` Steve Kargl
2015-07-10 16:20           ` Mikael Morin
2015-07-10 16:44             ` Andre Vehreschild
2015-07-10 18:57             ` Steve Kargl
2015-07-11 10:37               ` Andre Vehreschild [this message]
2015-07-11 11:06                 ` Mikael Morin
2015-07-11 11:58                 ` Dan Nagle
2015-07-13 17:27                   ` Mike Stump
2015-07-11 10:54               ` Mikael Morin
2015-07-11 10:58                 ` Andre Vehreschild
2015-07-11 15:37                 ` Steve Kargl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150711123648.1ac46e2a@vepi2 \
    --to=vehre@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=mikael.morin@sfr.fr \
    --cc=paul.richard.thomas@gmail.com \
    --cc=sgk@troutmask.apl.washington.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).