Home > Archive > Fortran > April 2005 > Problem with array size
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with array size
|
|
| D Marty 2005-04-24, 8:57 pm |
| Hello,
I don't understand why this following program fail and particulary why
the size of the array "A()" in subroutine "titi" is equal to zero.
Who know how can I have in subroutine "titi" the size of the array
"A()".
Thank you very much for your help.
D.Marty
Program toto
integer A(3)
A(1)=1
A(2)=2
A(3)=3
call titi(A)
end
subroutine titi(A)
integer A(*)
integer, ALLOCATABLE:: B(:)
allocate(B(size(A)))
B=A
write(*,*) B(1),B(2),B(3),size(A),size(B)
end
| |
| Ron Shepard 2005-04-25, 3:58 am |
| In article <2cc28924.0504241454.36cecc4@posting.google.com>,
nath.domi@tele2.fr (D Marty) wrote:
> Hello,
>
> I don't understand why this following program fail and particulary why
> the size of the array "A()" in subroutine "titi" is equal to zero.
>
> Who know how can I have in subroutine "titi" the size of the array
> "A()".
>
> Thank you very much for your help.
>
> D.Marty
>
>
> Program toto
> integer A(3)
> A(1)=1
> A(2)=2
> A(3)=3
> call titi(A)
> end
>
>
> subroutine titi(A)
> integer A(*)
> integer, ALLOCATABLE:: B(:)
> allocate(B(size(A)))
> B=A
> write(*,*) B(1),B(2),B(3),size(A),size(B)
> end
You need to do two things. First, change the declaration of the
dummy argument to
integer :: A(:)
Second, the subroutine needs what is called an "explicit interface".
There are several ways to do this:
* put the subroutine in a module, and "use" that module in the main
program.
* make the subroutine internal to the main program (i.e. with a
"contains" statement).
* add an interface block within the main program.
When programming in f95, you will do the first two of these fairly
often. The last should be avoided when possible because of the
possibility of making compatibility mistakes and because of
maintenance problems.
Finally, if you don't want to use an explicit "allocate" statement
for the array B(:) in the subroutine, you don't have to in this
case. It is simpler to declare an automatic array
integer :: B(size(A))
$.02 -Ron Shepard
| |
| Richard E Maine 2005-04-25, 3:58 am |
| In article <2cc28924.0504241454.36cecc4@posting.google.com>,
nath.domi@tele2.fr (D Marty) wrote:
> I don't understand why this following program fail and particulary why
> the size of the array "A()" in subroutine "titi" is equal to zero.
Ron gave some hints as to how to solve the problem. Let me comment
briefly on what the problem was
> subroutine titi(A)
> integer A(*)
The A(*) declaration here is an f77-style one known as assumed size.
Basically it means that the compiler is not told what the size is. You
aren't telling it in this declaration and it doesn't get it from
elsewhere either. Just because A is declared with a size of 3 in the
main program, that does *NOT* mean that this is known in the subroutine.
Actually, it is illegal to ask about the size of A at all in this case;
if you do ask, it isn't surprising to get a garbage answer such as 0.
The A(:) declaration that Ron suggests (and I concur) is known as
assumed shape. It means that the shape information (which includes size)
is passed from the main program to the subroutine. The explicit
interface that Ron described is necessary so that the main program can
know it is supposed to do it this way.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| D Marty 2005-04-25, 8:57 pm |
| Richard E Maine <nospam@see.signature> wrote in message news:<nospam-4EB389.17361324042005@news.supernews.com>...
> In article <2cc28924.0504241454.36cecc4@posting.google.com>,
> nath.domi@tele2.fr (D Marty) wrote:
>
>
> Ron gave some hints as to how to solve the problem. Let me comment
> briefly on what the problem was
>
>
> The A(*) declaration here is an f77-style one known as assumed size.
> Basically it means that the compiler is not told what the size is. You
> aren't telling it in this declaration and it doesn't get it from
> elsewhere either. Just because A is declared with a size of 3 in the
> main program, that does *NOT* mean that this is known in the subroutine.
> Actually, it is illegal to ask about the size of A at all in this case;
> if you do ask, it isn't surprising to get a garbage answer such as 0.
>
> The A(:) declaration that Ron suggests (and I concur) is known as
> assumed shape. It means that the shape information (which includes size)
> is passed from the main program to the subroutine. The explicit
> interface that Ron described is necessary so that the main program can
> know it is supposed to do it this way.
Thank you very much for your answers which are a big help for me.
D.Marty
|
|
|
|
|