View previous topic :: View next topic |
Author |
Message |
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Mon Jan 31, 2011 8:56 am Post subject: write longer string into shorter string |
|
|
Maybe somebody can give details on the following one:
there's code that writes a longer string into a shorter one using a '(aNUM)' formatting that looks fine at a first glance, but checkmate (or bounds_check) complain about it, most likely with good reason. Short test program:
Code: | program test
implicit none
character(len=20) :: short
character(len=50) :: long
long = '1234567890'
write(short, '(a20)') long
end program test |
Is there a real problem in the code? The intel compiler does not complain about this during compilation or runtime with maximal checkings enabled.
Thanks for any insight. |
|
Back to top |
|
|
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Mon Jan 31, 2011 10:33 am Post subject: |
|
|
Code: | program test
implicit none
character(len=20) :: short
character(len=50) :: long
long(1:10) = '1234567890'
write(short(1:10), '(a)') long(1:10)
! or
short(1:10)=long(1:10)
end program test |
|
|
Back to top |
|
|
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Mon Jan 31, 2011 11:37 am Post subject: |
|
|
Code: | write(short(1:10), '(a)') long(1:10) |
This is pretty much the code the original was changed into, which passes the checkmate tests. But what I'd like to know is if the original code really is buggy, or some insight into why the 'a(10)' isn't enough. |
|
Back to top |
|
|
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Tue Feb 01, 2011 9:26 am Post subject: |
|
|
The very same code works perfectly fine on XP (v6.0 ftn95 compiler) both in release mode as well as checkmate, and fails on Win7/64bit.
Further there is some pretty bad memory corruption due to this in some larger application (well-tested under XP and fails in semi-random manner when the non-cropped string is passed to write, and works fine when something like long(1:10) is used instead).
So this is most likely a bug in the ftn95 library. |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Feb 02, 2011 1:20 am Post subject: |
|
|
The original code looks wrong to me. The variable "long" is 50 characters long. You set the first 10 characters to something and the remaining 40 characters to blank (spaces). That is what the assignment does. It always did this in Fortran 77 and it hasn't changed in Fortran 95.
What happens if you try to write this to a character array "short" of length 20 using a20 formatting? Are the first 20 chararacters supposed to be written? Or should you get an error because the full 50 won't fit? |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Wed Feb 02, 2011 8:35 am Post subject: |
|
|
Sebastian,
short = long ! would achieve the same intention
You stated Quote: | The very same code works perfectly fine on XP (v6.0 ftn95 compiler) both in release mode as well as checkmate, and fails on Win7/64bit. |
Your original sample code looks ok to me, for the line you have quoted : "write(short, '(a20)') long"
I would expect it should be working ok.
I'm not sure if you say you have confirmed this is definately the cause of the problem, or could it be associated?
Have you proved with this simple sample you provided that it is failing in Win7/64 ?
Win7/64bit has different memory allocation and management to XP so if the error is in a larger program, you should consider that this is an error that is caused somewhere else and appearing at this time.
John |
|
Back to top |
|
|
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Wed Feb 02, 2011 8:51 am Post subject: |
|
|
Quote: | short = long ! would achieve the same intention |
The original code inserts a string into another one so it is not relevant to me to rewrite the sample. The posted code is a minimal sample to reproduce the bug.
Quote: | Have you proved with this simple sample you provided that it is failing in Win7/64 ? |
Yes, this is a default setup of a x64 machine running win7, and it was verified on other machines of the same architecture.
Quote: | The original code looks wrong to me. The variable "long" is 50 characters long. You set the first 10 characters to something and the remaining 40 characters to blank (spaces). |
Where is the "wrong" part? The assignment can be arbitrarily different, the faulting code is not related to that but the error only happens in the write statement.
Quote: | What happens if you try to write this to a character array "short" of length 20 using a20 formatting? |
You mean using character*20 instead of character(len=20) ? This results in exactly the same bug.
Thanks for the answers! |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Mon Feb 07, 2011 12:32 am Post subject: |
|
|
Sebastian,
Could the following work on Win 7 ?
write (short, '(a20)') long (1:20)
ie, making sure all arrays are the same length.
Making short longer, say a temporary character*80 may also remove the problem, eg
character temp*80
write (temp, '(a20)') long
short = temp
I don't yet have Windows 7 to test, but will soon, so this problem is of interest to me.
I would recommend that avoiding the use of write for character arrays in your main program would be the best approach. There may be a way.
If converting from numeric to character, I use a character function to localise this use. ( a function containing a write statement was probably not legal in F77 but does work for me.)
You may be able to identify where Win 7 is failing when doing these changes ??
John |
|
Back to top |
|
|
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Mon Feb 07, 2011 9:54 am Post subject: |
|
|
Quote: | Could the following work on Win 7 ?
write (short, '(a20)') long (1:20)
ie, making sure all arrays are the same length. |
Yes, see my first reply in this thread.
Quote: | I would recommend that avoiding the use of write for character arrays in your main program would be the best approach. There may be a way. |
There is no way to check the whole sources for this. And if the code is legal fortran (F90/F95) then there's no reason to change the code.
Quote: | You may be able to identify where Win 7 is failing when doing these changes ?? |
It's not Win7 that is failing, it's the Silverfrost C-library that is crashing on this. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Mon Feb 07, 2011 11:21 am Post subject: |
|
|
I will take a look at this shortly. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Wed Feb 16, 2011 1:27 pm Post subject: |
|
|
This code should never run for the reason given by davidb above.
Whether release or checkmate etc there will be a runtime failure |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Thu Feb 17, 2011 8:44 am Post subject: |
|
|
Paul,
I would have expectd that "write(short, '(a20)') long " would only require a 20 character buffer, as the format statement defines the output length, not "long"
John |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Thu Feb 17, 2011 8:51 am Post subject: |
|
|
OK I take the point but internal write is only an back-handed external write where the "file" is assumed to be large enough to take the data.
Maybe the I/O library could have been written differently or maybe it could be modified but this behavoiur seems natural to me. |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Thu Feb 17, 2011 9:06 am Post subject: |
|
|
I tried a variation of the original program and ran using /check and all appears to work ok. ( I'm still using Ver 5.40 on my notebook )
Code: | program test
implicit none
character(len=20) :: short
character(len=50) :: long
long = '123456789012345678901234567890'
write (short, '(a20)') long
write (*,*) short
write (*,*) short(1:2)
write (*,*) long
end program test |
John |
|
Back to top |
|
|
engrsandeep
Joined: 16 Feb 2011 Posts: 4
|
Posted: Thu Feb 17, 2011 10:23 am Post subject: |
|
|
Hey Guys, Jhonchappel, Sabetain, David, Paul...
Please help me out.... I have written my problem in last post like "Help me... I have problem in fortran"
And also Send me your email address. to send u that program to correct for me.. as I can run it.. man.
Please Please Help me out... my email address. engrsandeep@hotmail.com
regards,
Sandeep |
|
Back to top |
|
|
|