In Fortran, what is the difference between empty string and non-initialised string?

Viewed 586

character(20) :: a='', b When I define string a='', a gets a value of empty string. I do not initialise b, so b does not have value. I think the memory address have been allocated for a and b as they are defined. However, what is the difference bewteen empty sting and no-value?

3 Answers

Be aware of the unexpected behavior in Fortran that initialization in the declaration implies the save attribute which means that the initialization will only be effective on the first call to the function/subroutine, and whatever has later been written to the string will remain there on future calls. Making the function pure, elemental or recursive will result in a compile error.

In general, you should initialize after the declarations!

So in general a variable should only be declared like character(20) :: a. Now a is allocated in memory, and in general whatever was written in that sequence of memory will be what the variable contains until you assign to it. If you print it without first assigning, you will see a string interpretation of those bytes of memory.

for example

program t
    character (20) :: hej
    print*,hej
end program 

gives these random things, when running a few times (in ubuntu with gcc):

�Ʋ
86+
^�K
VBd�
&��A

In computing language, especially when it comes to memory, the notion of empty or no-value does not mean anything, unless you define what empty means. As my programming teacher once said, "in programming, anything that is not planned is necessarily planned"; meaning that whatever value you want in your variable, you make sure you assign it yourself, otherwise, it will be assigned for you for the compiler, the Operating system or other running programs. What they put there might not be what you want.

As Vladimir stated in his answer, if you do not define the value of the string, its value remains undefined. The undefined here simply means that whatever you do with that value is unpredictable. In other words, the first action you should do with that variable is to assign it a value.

The specialty of character string variables in programming is that they can be allocated more memory space than they are actually using. You can define it as character(20) :: a to reserve memory space for 20 characters and then use less than 20 characters; for example by assigning a single character a = '@', that is where the filling with trailing spaces comes into play. In that case, fortran fills the remaining 19 characters with the space ' ' character. Other programming languages use other mechanisms; for example, in C/C++, the recommendation is to use the null character to mark the end of the useful portion of the memory space; in Pascal/Delphi, a string variable carries along the number of actual characters in the string; etc.

With that explanation and your statement character(20) :: a='', b; the difference between a and b is that any action on a is predictable while the only predictable actions on b are actions that do not interpret its current value like assigning a value, querying the length, etc. Both a and b are allocated memory spaces, a is explicitly filled with trailing spaces, while the value in b is compiler dependent; it can be a default value (trailing spaces) or it can be anything. It is important to keep in mind the remark of Jonathan about the saved attribute. So if the variable a is defined in a function or subroutine, the value '' assigned at the declaration is guaranteed only on the first call. On any other call, a will have the last value set by the previous call.

If you do not define the value of the string, its value remains undefined. You are not allowed to access the value, you are not allowed print it. What will actually happen to be at that memory address depends on the internals of the implementation and on the location of the string. It may be zero characters, it may be some complete garbage.

If you define the value as empty "", the string will be filled with trailing blanks because Fortran adds trailing blanks when you assign to strings.

Related