Re: Short records on unformatted reads



Thomas Koenig <Thomas.Koenig@xxxxxxxxx> wrote:

What should the following program print?

program main
implicit none
integer, dimension(2) :: ia
open(10, form="unformatted", status="unknown")
write (10) 1, 'a'
rewind 10
ia = 42
read (10, err=20) ia
20 continue
print '(2I8)', ia
end program main

It is an illegal program, so the processor can do anything. You wrote an
integar and a character to the file. You then read two integers. That's
not legal (and is not likely to happen to work).

Note that the standard does not define I/O error conditions. It might
seem obvious to you (as it does to me) that this ought to count as an
error of teh kind that takes the err= branch, but the standard does not
require that. Most compilers probably will, but it isn't guaranted. I
once worked with a compiler that probably wouldn't for things like this.

Even if the err= exit is taken, the ia array becomes undefined in
standard speak. That's the whole array - not just the second element.
This means that it is illegal to subsequently reference it (until its
status changes).

If you reference it, the observed behavior may and probably does vary
among compilers. Thats' sort of the whole point of undefibnedness - to
avoid placing restrictions on compiler implementation in those cases.

Is the first number printed required to be 1?

No.

Is the processor free to print garbage as the second number?

Yes. Or to do anything else.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: Cant understand this! (Help required please)
    ... Yes and some online courses NETG does not seem to understand what ... > New to programming: ... I have heard of comeau don't they make compilers too? ... An argument I have used many times myself, I want to learn standard C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: GCC
    ... The header names changed explicitly to ... The Standard says that the standard ... that did not put iostream into the std namespace. ... and compilers change along with them. ...
    (Debian-User)
  • Re: subroutine stack and C machine model
    ... Herb has found out that a+bis ... Peter: But the STANDARD says... ... Most compilers have left to right evaluation as it happens. ... evidence appear to be more than one piece of evidence. ...
    (comp.lang.c)
  • Re: man 3 switch
    ... As C is an ISO standard, I sincerely doubt there would be any ... syntax and behaviour of the keywords between C compilers on any Unix-like ... of arithmetic operators of equal precedence in the same statement (in ... The languages themselves normally aren't. ...
    (Fedora)
  • Re: Is C99 the final C? (some suggestions)
    ... I for one would be happy if more compilers would ... this would require would be a standard minimum mantissa length. ... > supported by a mandatory compiler warning). ... > calls invocations that an implementation needs to support. ...
    (comp.lang.c)