Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29576 ディスカッション

String copying behavior

dannycat
新規コントリビューター I
1,831件の閲覧回数
Are there any problems caused by the following string statement:

character(len=8) :: shortstring
character(len=255) :: longstring

longstring = 'This string is longer than 8 characters'
shortstring = longstring

There are no compiler warnings or run time array bounds tripping even though these are enabled.

I have a program that behaves in a strange way where a user defined variablethat should never be changed (once initialised)is being altered during the run in release mode, though not in debug mode. The behavior suggests a memory overwrite somewhere and I am trying to rule out all possible causes.
Same problem occurs in 11.1.070 & 11.1.067 versions.

Any other suggestions (re optimisation switches to try etc.) would be most welcome.
0 件の賞賛
5 返答(返信)
Arjen_Markus
名誉コントリビューター II
1,831件の閲覧回数
Fortan is very robust against that sort of things. If the string on the right hand side is too long, then
it will be simply be truncated. My guess is that the problem you see is a consequence of another problem.

Do you have a small but complete program in which the error occurs? That would help reproducing it.

Regards,

Arjen
Robert_van_Amerongen
新規コントリビューター III
1,831件の閲覧回数
I guess there is neithera compiler warning nor a run-time error. Look at Metcalf c.s. Fortran95/2003 pag. 41 where it is explained that your shortstring variabele gets the value 'This str' (without ' of course).

Robert
mecej4
名誉コントリビューター III
1,831件の閲覧回数
There are no compiler warnings or run time array bounds tripping even though these are enabled

There are no arrays in this context, and the rules of Fortran are quite explicit in covering the assignment of a scalar string expression to a scalar string variable: padding with blanks if the variable is longer than the expression, truncation if shorter.

One is led to conclude that the source of the perceived error (clobbering of a set-once variable) lies elsewhere.
Steven_L_Intel1
従業員
1,831件の閲覧回数
All the replies are correct - there are no warnings or error messages for behavior strictly defined by the standard. Indeed, many applications depend on this behavior.

It can surprise some people when they do things like:

character(80) string
...
string = 'abcdef'
string = string // 'ghijkl'

and the second assignment has no effect.
abhimodak
新規コントリビューター I
1,831件の閲覧回数
Hi DannyCat

For your memory stomp issue perhaps the following may help:
Use a debug build with optimizations on. Keep the run-time check flags such as /check:bounds etc. as well. May be just use /check:all.

You may not get the exact location of stomp but there is a chance that you will get some useful information. Also try a setting up "data-breakpoint" for the variable in the optimized + debug + check mode.

Abhi
返信