Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28445 Discussions

Dynamically allocated character function

netphilou31
New Contributor II
330 Views
Hi,

I was wondering if it is possible (and how) to write a function that returns a character string whose size is dynamically allocated.
And if yes, how the character variable can be deallocated ?

Best regards,

Phil.

0 Kudos
4 Replies
IanH
Honored Contributor II
330 Views

Yes. Here's an example. Note that an explicit interface is required - your function either needs to be a module procedure, an internal procedure (as is here) or have an interface block in the calling scope.


[fxfortran]! Compile with /standard-semantics PROGRAM dynamically_allocated_char IMPLICIT NONE ! Syntax to declare a deferred length character variable. CHARACTER(:), ALLOCATABLE :: s !**** CALL RANDOM_SEED ! Length of s automatically set based on the length of the function ! result. The function result is allocatable - behind the scenes it ! will be deallocated automatically at the end of the "innermost ! executable construct" (here that's the program itself). s = function_that_returns_char_string() PRINT *, s ! You could explicitly deallocate s if you wanted to, or let it be cleaned ! up automatically when it goes out of scope. ! DEALLOCATE(s) CONTAINS FUNCTION function_that_returns_char_string() RESULT(str) ! Function result CHARACTER(:), ALLOCATABLE :: str ! Local variables REAL :: r INTEGER :: str_length !**** CALL RANDOM_NUMBER str_length = CEILING(r * 26.0) ! Length of str automatically set based on the length of the right ! hand side of the assignment statement. str = 'abcdefghijklmnopqrstuvwxyz'(:str_length) ! Alternatively - you could explicitly allocate the length... ! ALLOCATE( CHARACTER(str_length) :: str ) ! str(:) = 'abc...' END FUNCTION function_that_returns_char_string END PROGRAM dynamically_allocated_char[/fxfortran]
0 Kudos
netphilou31
New Contributor II
330 Views
Hi,

Thanks for the code sample, it worked has expected.
However what happen to the allocated string (str)in the function:
[fortran]FUNCTION function_that_returns_char_string() RESULT(str)[/fortran]once the result has been affected to the variable s ?
[fortran]s = function_that_returns_char_string()[/fortran]In my case I don't want to affect the result but I'd rather print directly the string.

Best regards,
0 Kudos
IanH
Honored Contributor II
330 Views
The function result (the thing called str inside the function body) is deallocated automatically at the end of the innermost executable construct that contains the function call. For this example, that's the end of the program.

(In this case, it doesn't really matter when that automatic deallocation happens, as it doesn't affect the program - the compiler could arrange for it to happen as soon as the assignment statement finished executing, or it could be clever and simply transfer the allocation from the result to the s variable, but that's implementation detail.)
0 Kudos
netphilou31
New Contributor II
330 Views
Thanks again,

I was just wondering if there was no memory leakage due to this particular use (especially when the function is called many times).

Best regards,

Phil.
0 Kudos
Reply