Re: Must declared length of a character function be matched by any procedure declaring it? YES
- From: "robin" <robin_v@xxxxxxxxxxx>
- Date: Tue, 01 Sep 2009 13:50:47 GMT
"John" <urbanjost@xxxxxxxxxxx> wrote in message
news:2a7f5e48-4679-4851-a188-b9d29d6d4a4b@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm wondering if I have developed an unneccessary habit. I have
assumed
that if I had a character function that was declared with an explicit
length that any routine that used the function had to declare it to be
of the same length (this goes back to pre-module/pre-interface days).
Somewhat in support of that someone was having problems with a large
mostly f77-ish code where strings were being returned with bad values
by a function where there was a mismatch in lengths; when changes were
made so the
length was consistent the problem went away.
But then I made a short test program while discussing this issue with
the code's developer, assuming the compiler would quickly support that
this was the case (by failing). But to my chagrin the following
little
test got no warning messages from any compiler I tried ...
That's because the code is in F77 mode, and errors like that usually
cannot be diagnosed.
program testit
character(len=8),external::bigfunc ! declare function as 8
characters
write(*,*)'string=['//bigfunc()//']'
end program testit
character(len=16) function bigfunc() ! function is defined as 16
characters
bigfunc='ABCDEFGHIJKLMNOP'
end function bigfunc
Try the following:
program test
character(len=8) :: t
t = bigfunc()
print *, t
print *, '[' // bigfunc() // ']'
contains
function bigfunc() result (s)
character(len=16) :: s
s = 'ABCDEFGHIJKLMNOP'
return
end function bigfunc
end program test
So if the function declares itself to be of one length, but references
to it use another, is the behavior just equivalent to
stringa=stringb
as far as truncation or padding to the right with blanks (depending of
course
on which string variable is bigger); or is this
prohibited?
Definitely prohibited. It always has been.
The reason that the error is not detected is that there is no explicit
interface.
Without an explicit interface, the compiler is relying on YOU to get it
correct.
If you don't get it correct, anything can happen because the program is
wrong.
Typical symptoms are wrong results or a a program crash.
Or, as you noticed, wrong results.
In Fortran 90 and later, a character function can return a string
of any length. The length can be constant, or it can depend on
the value or length of some argument. Don't forget to use an
explicit interface though.
The answer didn't leap out at me from the standard and I'm
hoping someone knows off the cuff with authority?
The good news for me would be that this code hasn't been doing
something
wrong for a considerable time; the bad news is that I will then be in
the position of not knowing why the changes that were made fixed the
problem -- and some of the answers are not good (probably smacking
memory
somewhere else, for example)
John Urban
.
- References:
- Prev by Date: Re: Returning Array Values by Single Assignment in External Function ??
- Next by Date: Re: Must declared length of a character function be matched by any procedure declaring it?
- Previous by thread: Re: Must declared length of a character function be matched by any procedure declaring it?
- Next by thread: Re: Must declared length of a character function be matched by any procedure declaring it?
- Index(es):
Relevant Pages
|