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

string parameter overflow

andrew_4619
Honored Contributor III
2,126 Views

This might be a stupid question but I just found a bug in something I wrote earlier today and I was surprised the complier didn't have a little moan at me.

      character(12), parameter :: gudef(5)=(/'@@USER-DEF-1@@','@@USER-DEF-2@@','@@USER-DEF-3@@','@@USER-DEF-4@@','@@USER-DEF-5@@'/)

Those 14 character long strings don't fit that well in the 12 character spaces... 

It would whinge at me if they weren't all this same length but all the wrong length is OK?

0 Kudos
9 Replies
FortranFan
Honored Contributor III
2,126 Views

Since all the elements are of the same length, one can now do:

   character(len=*), parameter :: gudef(5) = [ '@@USER-DEF-1@@', '@@USER-DEF-2@@',                  &
                                               '@@USER-DEF-3@@', '@@USER-DEF-4@@',                  &
                                               '@@USER-DEF-5@@' ]

 

0 Kudos
FortranFan
Honored Contributor III
2,126 Views

And if the elements are of different length, the following makes it easier:

   character(len=*), parameter :: s(3) = [ character(len=6) :: "foo", "bar", "foobar" ]

The type-def in the array expression has to specify the longest length expected, of course.

0 Kudos
Steven_L_Intel1
Employee
2,126 Views

The standard says, "The entity has the value specified by ts constant-expr, converted, if necessary, to the type, type parameters and shape of the entity."  The rules of such conversion include truncation of character strings. I agree that it would be nice for the compiler to warn you about this - I will suggest it.

0 Kudos
andrew_4619
Honored Contributor III
2,126 Views

Thanks FF #2 is what I should have done, I tend to code such things on autopilot and adding the length rather than * in this instance was one of those. I wasn't aware of the syntax of #3 and yes that is quite useful also but not for the current activity.

Steve, thanks not a high priority I know but as you say it would be nice because I think 19 times out of 20 it would expose a code bug at compile time.

0 Kudos
Blane_J_
New Contributor I
2,126 Views

FortranFan wrote:

And if the elements are of different length, the following makes it easier:

   character(len=*), parameter :: s(3) = [ character(len=6) :: "foo", "bar", "foobar" ]

The type-def in the array expression has to specify the longest length expected, of course.

I'm intreasted in the sytax here. Could you please suggest a reference to look into it ? Is there any defference between (/ & /) AND [ & ] ?

0 Kudos
FortranFan
Honored Contributor III
2,126 Views

Blane J. wrote:

 ..

I'm intreasted in the sytax here. Could you please suggest a reference to look into it ? Is there any defference between (/ & /) AND [ & ] ?

See the references in Dr Fortran's blog: https://software.intel.com/en-us/blogs/2013/12/30/doctor-fortran-in-its-a-modern-fortran-world.  For the point in this thread, consult Metcalf et al., "Modern Fortran Explained".

In array constructors, you can now pretty much use [ .. ] instead of (/ .. /); Metcalf et al, explains this in more detail.

0 Kudos
andrew_4619
Honored Contributor III
2,126 Views

I use array constructors [..]  in code all the time these days but in declarations assignments use (/.../). Am I correct in saying that until quite recently ifort did not accept  [....] in declaration initialisation assignments

0 Kudos
Steven_L_Intel1
Employee
2,126 Views

We have accepted [] as alternatives to (/ /) "forever", long before it became standard in F2008 and including in initialization expressions.

Being able to specify the type in an array constructor was in F2003.

0 Kudos
andrew_4619
Honored Contributor III
2,126 Views

Steve Lionel (Intel) wrote:

We have accepted [] as alternatives to (/ /) "forever", long before it became standard in F2008 and including in initialization expressions.

Being able to specify the type in an array constructor was in F2003.

Interesting maybe when I last did that I goofed in some other way and incorrectly assumed that the syntax was not supported in that context....

 

0 Kudos
Reply