- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I was just surprised by a bug in my code about which I did not get a warning:
character(LEN=4) :: name
name = "DAVID"
There is no compiler warning that "DAVID" gets truncated when stored in name.
Is there any compiler setting to force this warning? I could not see one.
Thanks,
David
Link Copied
- « Previous
-
- 1
- 2
- Next »
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Luigi,
That is valid.
gvautier was illustrating how, by declaring a dummy argument of subroutine (without interface checking), that you can declare a character string (or any other array) to be larger than the actual argument. He is cautioning you about this characteristic of Fortran. To avoid this, use interface checking (or not use it if you really need to).
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Luigi R. wrote:
This discussion seems a bit absurd.
If I have a char*80 string VTEXT and want to analyze the first 8 chars why should not write:
character*8 code
code = vtext
Luigi,
I would prefer to have at least an option so that the compiler issue a warning for your example.
Why not code this explicitly
code = vtext(1:len(code))
If you only want the first 8 characters.
It seems to me that explicitly coding what you want to happen is better than hoping that it will.
Your example could be a typo, in that you meant to declare code as 80 characters long to match vtext, but declared it incorrectly.
We have many other warnings like this. And as other posters have indicated gfortran and silverfrost to detect this and issue a warning.
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There is the usual trade-off for a language (computer or natural) between the inconvenience of the verbosity (and some forms of verbosity result in a loss of clarity and are somewhat associated with the chance of making an error) of having to be explicit about something, and the convenience but possibility of misinterpretation or mis-intention of an implicit action. In a standard Fortran context the implicit conversions between integer and real, or the implicit conversions between different kinds of a particular intrinsic type are similar trade-offs.
A little care is needed with something like `code = vtext(1:len(code))` - it assumes that the length of code is always less than or equal to the length of vtext. That's the sort of assumption that could break as a code evolves - make sure your explicit medicine is not worse than the disease. So to be robust and explicit what you want is `code = vtext(:min(len(code),len(vtext))`, which is perhaps an example of where verbosity negatively impacts clarity. That still relies on the implicit rules of character length parameter conversion, when len(vtext) is less than len(code).
From the point of view of a warning I think there is a difference between assignment of a literal constant to a shorter fixed length string and assignment of a more general expression, though `name = name // other` is also questionable.
To be mischievous, use of any fixed length character variable (apart from those with a length of one) should probably cop a warning these days...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- « Previous
-
- 1
- 2
- Next »