Silverfrost Forums

Welcome to our forums

Automatic CHARACTER storage; unexpected behaviour

26 Aug 2010 4:22 #6841

Please can someone have a look at this little program and tell me whether it is me or FTN95 that is being very stupid?

Step into the subroutine and observe carefully. The automatic character variable charac does not get assigned, character by character, to char (0). Instead, as soon as the assignment statement is hit for the first time, characters 5 to 10 of charac get set to something different, and charac does not change for the rest of the loop. When the end of the subroutine is reached, charac reverts to what it was on entry.

I can't see anything wrong with the code, but equally I can't believe that a compiler bug this blatant would have escaped detection 😒hock:

        program deferred
        integer size
        size = 4
        call sub (size)
        end program deferred
        subroutine sub (n)
        integer i, n
        character (len = n) :: charac
        do i = 1, n
          charac (i: i) = char (0)
        end do
        end subroutine sub
26 Aug 2010 5:25 #6843

I think that it is just the debugger that cannot cope...

        program deferred
        integer size
        size = 4
        call sub (size)
        end program deferred
        subroutine sub (n)
        integer i, n
        character (len = n) :: charac
        do i = 1, n
          charac (i: i) = char(i-1)
        end do
        do i = 1, n
          print*, ichar(charac(i:i))
        end do
        end subroutine sub
26 Aug 2010 11:58 #6844

Hmmm. ... yes, it looks that way, with your print modification. But what's not to cope with? It must count as a debugger bug, surely. I see now in my original post, I talked about characters 5 to 10 getting changed when the loop is entered, when charac is only 4 characters long. That's because originally I had size = 10, and cut it down to abbreviate the stepping through in sdbg, but didn't amend the message. That behaviour is obviously system dependent. Here at home, if I set size back to 10, it's only characters 5-8 of charac that change, and what they change to is the value of n. At work, it was the value of i that they changed to (I could see it incrementing as I stepped through the loop).

So sdbg seems to be displaying data at some spurious address. It actually displays the same data whether I ask for memory dump at variable, or memory dump using contents. The address of the data that sdbg displays for charac is nearly the same as the data that is occupying that address, but not the same. On this machine, the address of the data displayed is 0x0360fcd8 and the data displayed is 0x0360fbb8. So I ought to get two different displays depending which flavour of memory dump I ask for, but I don't.

Please login to reply.