1

I'm having a runtime error when I run a code that works without problems using a different computer.

I'm wondering if the problem is the Fortran compiler of this machine (GCC 4.9.2) since the former computer used a previous version.

The issue comes when defining a variable like this:

In a module I define

character(30),allocatable,save :: sceneclass(:)

Then in the subroutine sceneclass is defined according to

character(30) surf, frac, scene

allocate(sceneclass(10)) 

do i=1,10
write(sceneclass(i),*) trim(scene)//trim(surf)//'_'//trim(frac)
enddo

In the first iteration I get the "End of record". But I don't know where is the problem. It seems to work fine in other computers.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
cardogar
  • 359
  • 1
  • 4
  • 17

2 Answers2

1

You are probably writing a string to sceneclass(i) that is longer then the 30 chars you specified.

I can reproduce this with

program test
  implicit none
  character(10),allocatable :: sceneclass(:)
  integer                   :: i

  allocate( sceneclass(10) ) 

  do i=1,10
    write(sceneclass(i),*) 10**i
  enddo

  print *, ( trim(sceneclass(i)), i=1,10 )

end program

gfortran fails with

Fortran runtime error: End of record

while ifort reports the error correctly:

output statement overflows record, unit -5, file Internal List-Directed Write

Increasing the string length to 12 solves the issue in this case. You can (and should) use iostat within the write statement to capture this.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • I created a [feature request/bug report](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65684) at the GCC site. – Alexander Vogt Apr 07 '15 at 11:46
  • That was the issue. For same reason, blanks weren't a problem using previous gcc fortran versions. Thanks! – cardogar Apr 07 '15 at 12:15
  • @cardogar Blanks should not be a problem! BTW this is why I asked what is in those variables you write because it looked like an overflow. You should always supply complete information in your question. And I don't think the error message is wrong, it is more or less equivalent to the intel's one. – Vladimir F Героям слава Apr 07 '15 at 12:27
  • @VladimirF Probably, but it is misleading (in my eyes). Intel's error message directly leads to the cause of the problem. – Alexander Vogt Apr 07 '15 at 12:34
  • I could imagine some compiler blindly over writing into whatever is adjacent in memory. Could you be specific what old version accepted this? – agentp Apr 07 '15 at 15:10
1

One thing to be aware of, when you specify a list directed * write of a character string, the compiler always(?) adds an extra lead blank, so the effective length of the string you can write is one less than you might expect.

To remedy that (and of course assuming you don't want the lead blank anyway ) use a string edit descriptor:

   write(sceneclass(i),'(a)')...

Interestingly ifort (linux 11.1) actually allows you to overrun by one character:

 character*5 c
 write(c,*)'12345'  ! result: " 1234"

which I would consider a bug. It seems they forgot to count the blank too.. ( gfortran throws the above error on this, and ifort balks if you add one more character )

see here if you wonder why the blank.. Are Fortran control characters (carriage control) still implemented in compilers?

and now I'm curious if some compiler somewhere didn't do that for an internal list write, or maybe your code was previously compiled with a flag to disable the "printer control" code

Community
  • 1
  • 1
agentp
  • 6,849
  • 2
  • 19
  • 37