Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

String copying behavior

dannycat
New Contributor I
1,106 Views
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 Kudos
5 Replies
Arjen_Markus
Honored Contributor II
1,106 Views
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
0 Kudos
Robert_van_Amerongen
New Contributor III
1,106 Views
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
0 Kudos
mecej4
Honored Contributor III
1,106 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,106 Views
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.
0 Kudos
abhimodak
New Contributor I
1,106 Views
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
0 Kudos
Reply