View previous topic :: View next topic |
Author |
Message |
brucebowler Guest
|
Posted: Fri Feb 24, 2006 1:32 pm Post subject: Inonsistent behaviour when passing an section of an array to |
|
|
Can you tell me why the following piece of code prints 10, instead of 5? If it really is 10, why does an attempt to access element 6 result in an array bounds error???
program test
real a(10)
forall (i=1:10) a(i) = real(i)
call sub(a(1:5))
stop
end
subroutine sub(a)
real a(*)
write (6,*) size(a)
return
end
Thanks!
Bruce |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Fri Feb 24, 2006 3:25 pm Post subject: Inonsistent behaviour when passing an section of an array to |
|
|
Bruce
You get the right answer when you use an interface and an assumed shape array.
I would have to check the Fortran 77 Standard to see if FTN95 is behaving correctly for your assumed size array.
program test
interface
subroutine sub(a)
real a(
end subroutine
end interface
real a(10)
forall (i=1:10) a(i) = real(i)
call sub(a(1:5))
stop
end
subroutine sub(a)
real a(
write (6,*) size(a)
return
end
|
|
Back to top |
|
|
brucebowler Guest
|
Posted: Mon Feb 27, 2006 7:12 am Post subject: Inonsistent behaviour when passing an section of an array to |
|
|
Paul,
But the question you left unanswered (which was, I guess, the "real" question) is - If I try and access array element 6 in SUB, I get an "array out of bounds" error. *THAT* is inconsistant with size(a) = 10. I know I can fix it by using interfaces, but am curious about the inconsitancy.
Thanks!
Bruce |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Mon Feb 27, 2006 10:32 am Post subject: Inonsistent behaviour when passing an section of an array to |
|
|
Bruce
I tried your code on a third party compiler and it gave a very large value for the size.
As far as I can tell from a quick inspection, the Fortran 90 standard does not say what should happen when a (Fortran 90) array section is passed as an assumed-size (Fortran 77) argument.
The standards committee probably worked on the assumption that array sections would be passed as assumed-shape arrays.
I do not think that we can class this as a bug.
The amount of work needed to "fix" the compiler (to give the more intuitive result) would depend on whether or not the required information is immediately to hand. Chances are that the information is not to hand and that this is why the compiler writer stuck to the old standard and simply passed the size of the whole array. If a change were feasible, we would also have to assess the possibility that we might break some existing user's code by making a change.
In the mean time it is apparent that array sections must be passed as assumed-shape arrays for FTN95 and for portability to other compilers.
Now perhaps to the real answer to your question...
FTN95 passes additional hidden arguments in /CHECK mode etc. These are used to provide runtime checking (of array bounds etc) during development. The additional information is not available for use in the SIZE intrinsic which has to work for both checked and unchecked code.
|
|
Back to top |
|
|
|