๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.fortran
Article #158

Re: โ€œThe end of software secrecy is almost upon

#158
From: Steven G. Kargl
Date: Fri, 23 Oct 2026 01:57
67 lines
4079 bytes
On Thu, 22 Oct 2026 16:54:12 -0700, Keith Thompson wrote:

> Thomas Koenig <tkoenig@netcologne.de> writes:
> [...]
>> Somebody who knows the first thing about Fortran, which you
>> clearly don't, might have realized that scope is the only
>> way to mandate storage allocation/deallocation in Fortran.
> 
> Is that true in more recent versions of Fortran?
> 
> A quick web search indicates that Fortran 90 added "allocate"
> and "deallocate".  If I understand correctly, you might have an
> "allocate" inside a DO loop, and breaking out of the loop could
> bypass the corresponding "deallocate".
> 

As Thomas indicated, it depends on scope.  A function is
a scoping unit.  For this simple function,

   function foo(n)
     integer foo
     integer, intent(in) :: n
     integer, allocatable :: i(:)
     allocate(i(n))
     do j = 1, n
	i(j) = j
     end do 
     foo = sum(i)
   end function

'i' is a local variable, which is allocated to have 'n' elements.
When the function returns, Fortran will automatically deallocate
'i'.  A do-loop is not a scoping unit, so there is no deallocation
when a loop ends.  Here's a function that has an allocate in 
the do-loop

   function foo(n)
     integer foo
     integer, intent(in) :: n
     integer, allocatable :: i(:)
     do j = 1, n
        allocate(i(j))   ! allocate i to have j elements
	i = j		 ! set all elements to j
	print *, sum(i)
        if (j == n / 2) exit
	deallocate(i)    ! free for next loop iteration
     end do 
     foo = sum(i)        ! On exit from loop, i is still allocated
   end function

If the if-condition is false, the user must deallocate 'i'
for the next iteration of the loop.  Fortran does not have a
realloc() function.  When the loop is exited on a true 
condition, 'i' is still allocated and can be referenced in
the last assignment statement to foo.  Finally, when the 
function returns, 'i' is deallocated as its a local variable.

It can be a bit more complicated with some newer Fortran 
features such as ASSOCIATE and BLOCK constructs that define
scoping units with a scoping unit.  But, in general a local
entity that is allocated with a scoping unit will be deallocated
when the scoping unit goes out-of-scope.

PS: trimmed the rust and folklore newsgroups as this is 
irrelevant to those.
-- 
steve
Technical Details
Message-ID: <11beeul$1cbdr$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11amvre$15far$1@dont-email.me> <11b5tdb$2cs5l$1@dont-email.me> <11b64cp$end$1@panix2.panix.com> <11b64hj$2faos$1@dont-email.me> <11b679m$6dl$1@panix2.panix.com> <11b6vc4$2npqv$1@dont-email.me> <11b87jg$37b7t$1@dont-email.me> <11b884b$p2o9$1@dont-email.me> <11b94he$3hdo3$2@dont-email.me> <11b9qd6$pq4i$1@dont-email.me> <11b9r5r$3nppf$1@dont-email.me> <11baekk$3v7n1$1@dont-email.me> <11bbl4f$du80$2@dont-email.me> <11bc0um$h2uu$1@dont-email.me> <11bc4nv$i1rd$2@dont-email.me> <11bcb2k$jqfh$1@dont-email.me> <11be7n7$1abv3$2@kst.eternal-september.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu>
Newsgroups: comp.lang.fortran,comp.lang.c++
Subject: Re: โ€œThe end of software secrecy is almost upon
Date: Fri, 23 Oct 2026 01:57:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <11beeul$1cbdr$1@dont-email.me>
References: <11amvre$15far$1@dont-email.me> <11b5tdb$2cs5l$1@dont-email.me>
	<11b64cp$end$1@panix2.panix.com> <11b64hj$2faos$1@dont-email.me>
	<11b679m$6dl$1@panix2.panix.com> <11b6vc4$2npqv$1@dont-email.me>
	<11b87jg$37b7t$1@dont-email.me> <11b884b$p2o9$1@dont-email.me>
	<11b94he$3hdo3$2@dont-email.me> <11b9qd6$pq4i$1@dont-email.me>
	<11b9r5r$3nppf$1@dont-email.me> <11baekk$3v7n1$1@dont-email.me>
	<11bbl4f$du80$2@dont-email.me> <11bc0um$h2uu$1@dont-email.me>
	<11bc4nv$i1rd$2@dont-email.me> <11bcb2k$jqfh$1@dont-email.me>
	<11be7n7$1abv3$2@kst.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 23 Oct 2026 01:57:42 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1453499"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19RDEeDCdQYHPHMtHdUsyOb"; posting-host="472bb57484ceabb0e777fdb603a52539"
User-Agent: Pan/0.164 (Kupiansk)
Cancel-Lock: sha1:M2F6wZ52rXn4eU+Q+hNrB0bPSlY= sha256:U+/nAwf4SXhRYuHoEavj/2gDOdAdPy2DZIaZ+eUj6yQ=
	sha1:nHYaptAB0gIKt2XlMpWx3aMg3xk= sha256:bQN6BJD1ixQSqBgdeCDF9Y+vTtNbL4m9Ku2cAXFJVT8=