(j3.2006) Question about zero-sized array.
Malcolm Cohen
malcolm
Tue Jun 20 19:52:25 EDT 2017
Daniel Chen asks:
> The standard specifies, "Zero-sized arrays and zero-length strings are
> always defined".
> It also says "Allocation of a zero-sized array or zero-length character
> variable causes the array or variable to become defined"
>
> Question1: Does it have initial value at all?
John Reid opines:
>Well, yes. It always has the same value, including initially.
No. Being allocatable or a pointer it is initially undefined. ?initially? means at the beginning of execution; at that point the array does not have any shape.
Daniel continues::
> allocate(a(7,0),x(0),y(7))
> a(:,:) = 0.0 !Q2: Is this legal? What is the value of a?
John replies:
>Yes, you are broadcasting a scalar to every element of the array. a has
>the value it always has when the size is zero.
?it always has? could be a little misleading here; being an array, the shape is part of the value, so the value is the 7x0 empty array and not the 0x23 empty array. I?m not sure we have all this explicitly written down anywhere though, except for derived types, where we say the array bounds of a component are part of the value of the structure. Outside of a structure it is the shape that?s important rather than the bounds.
Cheers,
--
..............Malcolm Cohen, NAG Oxford/Tokyo.
From: j3-bounces at mailman.j3-fortran.org [mailto:j3-bounces at mailman.j3-fortran.org] On Behalf Of John Reid
Sent: Wednesday, June 21, 2017 12:53 AM
To: fortran standards email list for J3 <j3 at mailman.j3-fortran.org>
Subject: Re: (j3.2006) Question about zero-sized array.
Daniel C Chen wrote:
> Hello,
>
> The standard specifies, "Zero-sized arrays and zero-length strings are
> always defined".
> It also says "Allocation of a zero-sized array or zero-length character
> variable causes the array or variable to become defined"
>
> Question1: Does it have initial value at all?
Well, yes. It always has the same value, including initially.
> Question2: Can it appear as the LHS of an assignment?
Yes.
> Question3: Can it be used in an expression and have a defined result?
Yes.
>
> Please consider the following example:
>
> program test
> real, dimension(:,:), allocatable:: a
> real, dimension(:), allocatable :: x,y
>
> allocate(a(7,0),x(0),y(7))
> a(:,:) = 0.0 !Q2: Is this legal? What is the value of a?
Yes, you are broadcasting a scalar to every element of the array. a has
the value it always has when the size is zero.
> x(:) = 0.0
Yes, you are again broadcasting a scalar to every element of the array.
> y(:) = 1.0
> y(:) = MATMUL( a(:,:), x(:) ) !Q3: Is this legal? what is the value of y?
Yes. Every element of y has the value 0.0.
> end program test
>
> Non-allocatable version:
Nothing is reallocated in the allocatable version, so this is the same.
Cheers,
John.
> program test2
> real :: a(7,0)
> real :: x(0),y(7)
>
> a = 0.0 !Q2: is this legal? What is the value of a?
> x = 0.0
> y = 1.0
> y = MATMUL( a, x ) !Q3: is this legal? what is the value of y?
> end program test2
>
> Thanks,
>
> Daniel
>
> XL Fortran Development, Fortran Standard Representative
> IBM Toronto Software Lab
> Phone: 905-413-3056
> Tie: 969-3056
> Email: cdchen at ca.ibm.com <mailto:cdchen at ca.ibm.com>
> http://www.ibm.com/software/awdtools/fortran/xlfortran
>
>
> _______________________________________________
> J3 mailing list
> J3 at mailman.j3-fortran.org <mailto:J3 at mailman.j3-fortran.org>
> http://mailman.j3-fortran.org/mailman/listinfo/j3
>
_______________________________________________
J3 mailing list
J3 at mailman.j3-fortran.org <mailto:J3 at mailman.j3-fortran.org>
http://mailman.j3-fortran.org/mailman/listinfo/j3
Disclaimer
The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.
This e-mail has been scanned for all viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.j3-fortran.org/pipermail/j3/attachments/20170621/98a0ecf2/attachment.html
More information about the J3
mailing list