Re: String in Fortran

Re: String in Fortran

Jugoslav Dujic's picture

For interaction with which language?

Seriously, CHARACTER*(*) means that any memory associated with STR_OUT is allocated in the caller and info about amount in that memory is passed to Fortran. So, you can't return string of variable length in Fortran as you couldn't in any language because it isn't your routine that takes care about the memory. What you can do is to write something into that buffer whatever the size is.

On the other hand, if you mean "how not to expect length argument from the caller", it is possible:

SUBROUTINE FSub(str_out) 
!DEC$ATTRIBUTES REFERENCE:: str_out 
CHARACTER(100):: str_out 


But note that this means "I expect this buffer not to be smaller than 100". If it actually was, you might easily overwrite "caller's" memory and do some nasty thing.

Note that, for example, almost all Win32 API functions which fill in some string always have an additional length argument which the caller should fill in (e.g. RegQueryValueEx) (although strings are always passed by reference there); that ensures that an error will be returned if the buffer is too small, not that a silent overwrite would occur gobbling the memory.

Jugoslav

Jugoslav www.xeffort.com
1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.