[J3] Are array bounds of an allocatable function result persistent?

Daniel Chen cdchen at ca.ibm.com
Fri Feb 7 20:05:51 UTC 2025


I see Steve’s logic now. Thank you all for responding!

Daniel

From: J3 <j3-bounces at mailman.j3-fortran.org> on behalf of Steve Lionel via J3 <j3 at mailman.j3-fortran.org>
Date: Friday, February 7, 2025 at 2:30 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Steve Lionel <steve at stevelionel.com>
Subject: [EXTERNAL] Re: [J3] Are array bounds of an allocatable function result persistent?
Sorry, I was looking at the wrong thing. In this case we simply need to look at the definition of LBOUND and UBOUND, recognizing that foo() is an expression and not a "whole array" ("array component or array name without further qualification"),

Sorry, I was looking at the wrong thing. In this case we simply need to look at the definition of LBOUND and UBOUND, recognizing that foo() is an expression and not a "whole array" ("array component or array name without further qualification"), in which case "1 3" falls out.

Steve

On 2/7/2025 2:15 PM, Shafran, Aury wrote:
Hi Steve,

I agree with your answer, but I’m confused by your explanation / quote from the standard. You’re quoting the section for the behavior of intrinsic assignment, but I don’t think intrinsic assignment is related to the question.

The assignment “foo = 1” doesn’t cause reallocation of the allocatable because the RHS a scalar. Earlier in the same paragraph you quoted, it says the things that cause the variable to become deallocated, and the RHS being a scalar isn’t one of them. For example, this program prints “4  6”:

real, allocatable :: foo(:)
allocate(foo(4:6))
foo = 1
print *, lbound(foo)
print *, ubound(foo)
end

The question is about the function result, and the “foo = 1” in Daniel’s original question isn’t necessary to see the result “1  3”. You can remove that assignment and the result of the program is the same.

But yes, a function result is an expression, and I think an expression has bounds (or, non-default bounds, at least), it only has a shape:

10.1.1 p1: “Evaluation of an expression produces a value, which has a type, type parameters (if appropriate), and a shape.”

I think it’s similar to doing “lbound(foo + 1)” (inside the function foo) which will also be 1.
But that answer still does feel hand-wavy to me. I’m not certain exactly how the standard says the bounds get lost in the function result.

Thanks,
Aury


From: J3 <j3-bounces at mailman.j3-fortran.org><mailto:j3-bounces at mailman.j3-fortran.org> On Behalf Of Steve Lionel via J3
Sent: Friday, February 7, 2025 1:56 PM
To: j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>
Cc: Steve Lionel <steve at stevelionel.com><mailto:steve at stevelionel.com>
Subject: Re: [J3] Are array bounds of an allocatable function result persistent?

On 2/7/2025 1:03 PM, Daniel Chen via J3 wrote:


I would expect 4, 6 but couldn’t’ find any wording in the standard that specifies one way of the other..



10.2.1.3p3: If the variable is or becomes an unallocated allocatable variable, it is then allocated with ... the shape of expr with each lower bound equal to the corresponding element of LBOUND (expr) if expr is an array.

A function result is an expression, not a variable, so a lower bound of 1 is correct.

Steve


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20250207/82e73638/attachment.htm>


More information about the J3 mailing list