Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Problem with array size
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

Report this thread to moderator Post Follow-up to this message
Old Post
D Marty
04-25-05 01:57 AM


Re: Problem with array size
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

Report this thread to moderator Post Follow-up to this message
Old Post
Ron Shepard
04-25-05 08:58 AM


Re: Problem with array size
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

Report this thread to moderator Post Follow-up to this message
Old Post
Richard E Maine
04-25-05 08:58 AM


Re: Problem with array size
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

Report this thread to moderator Post Follow-up to this message
Old Post
D Marty
04-26-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:21 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.