Is there a way to programatically initialize derived types or structures that are automatic? I created a C function which accepts a CFI_cdesc_t * parameter, and then uses:
memset( a->base_addr, 0, a_elem_len );
...to initialize the passed argument. However, if the argument has elements that are of type CHARACTER, it will obviously initialize them to NULLs instead of spaces (which is the default when "-save -zero" are used as compile flags). Is there a RTL function that can be called to initialize a structure in the same manner as if "-zero" were specified (e.g. strings initialized to spaces)?
P.S. -- I would prefer to avoid DATA /FOO/ tricks, because that causes the structure to appear in the BSS section, which was the whole reason for going with automatic variables in the first place.
Look in the documentation under
user-defined TYPE statement
! The following program shows how you initialize components of a derived-type object: TYPE list_node CHARACTER(20) :: street_name = “ “ INTEGER(2) :: street_number = 0 INTEGER(2) :: apt_number = -1 CHARACTER(20) :: city = “ “ CHARACTER(2) :: state = “NH” INTEGER(4) :: zip = 0 TYPE (list_node), POINTER :: next => NULL() END TYPE TYPE (list_node) :: x = list_node (zip = 03054) PRINT *, x%state, x%zip END
To answer your question, there isn't a function that can automatically choose whether to initialize with zeros or spaces. You have the datatype in the C descriptor so it isn't hard to make that test yourself in your memset call.
Jim correctly shows how to declare a Fortran structure that is initialized automatically when a variable of its type comes into existence. (An interesting thing about this is that INTENT(OUT) dummy arguments of such a type get initialized at each entry to the routine.)
In the above example, it is specifically initializing "next" under the presumption that the uninitialized pointer would be junk data.
Assume you have a user defined type without initialization, further assume the type has an allocatable array.
Are user defined type allocatables implicitly initialized to an unallocated state? It would seem like they would have to be initialized otherwise the junk may appear as allocated (to valid or invalid addresses).
Interesting information about the INTENT(OUT).
On allocation of a user-defined type, if any of the type components have default initialization specified, those components get initialized and others do not. Allocatable components in a derived type do get set to an unallocated state.