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

Default initialization for strings

Andrew_Smith
Valued Contributor I
490 Views
I came accross a problem with variable length strings. It you assign an unallocated string to another string you get an error.

So I thought what use is it really having an unallocated state for them when an allocated state of zero length would probably take up the same memory and be more robust. But I guess some like having the unitialized state as a debugging aid.

In my app not all instances get initialized on purpose. So I tried to provide a default initializer just like you can with a non-allocatable data types. But this turned out to be illegal. Why was this restriction for allocatables not dropped for allocatable character strings ?

[bash]type Animal
character(:), allocatable :: name = ''
end type[/bash]

How about a new string type that totally hides all the allocation stuff.

[bash]type Animal
string :: name = ''
end type[/bash]
0 Kudos
1 Reply
mecej4
Honored Contributor III
490 Views
Let us consider the corresponding situation with integers:
[fortran]module allbmod
type Amphib
   integer, allocatable :: frog(:)=(/ 1, 2 /)
end type
end module allbmod[/fortran]
How would you want the compiler to treat this code?

Remember that initialization of an allocatable component in a derived type definition is not permitted by the standard.
0 Kudos
Reply