Article View: comp.std.c
Article #2102Re: fwrite+fread of pointer
From: Norman Diamond
Date: Fri, 13 Nov 1992 10:18
Date: Fri, 13 Nov 1992 10:18
40 lines
2522 bytes
2522 bytes
In article <15935@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> #include <stdlib.h>
> #include <stdio.h>
> int main(argc, argv) int argc; char **argv; {
> char dummy;
> char *p = &dummy;
> FILE *f = fopen("WORK", "w+b");
> fwrite(&p, sizeof p, 1, f);
> rewind(f);
> fread(&p, sizeof p, 1, f);
> fclose(f);
> printf("%d\n", p == &dummy);
> exit(0);
> }
>What I want to know is whether, assuming nothing else goes wrong, the
>program _must_ print 1 or whether it may print 0.
ANSI 4.9.2, page 126 lines 33 to 34: "Data read in from a binary stream
shall compare equal to the data that were earlier written out to that
stream, under the same implementation." So I almost say it must print 1.
The clause "under the same implementation" can only mean that the file
might be read by a different program (or different invocation of the same
program) than the one that wrote the file, and as long as those were
translated by the same implementation, the data must remain equal.
But of course, in a different program, the variable "p" is likely to be
at a different location. We should reasonably expect this to be an
exception, but the standard doesn't hint at an exception. This troubles me.
The real reason I am troubled is that until now, I thought that it was
theoretically possible to implement garbage collection in C. Each pointer
would have to be a handle and each object would need additional information
so that when an object is moved, all of the pointers in the program could
be updated automatically. But this would yield a second exception to this
part of the standard -- and it could yield a 0 for Dr. O'Keefe's program.
Perhaps an interpretation ruling should say how many exceptions are allowable.
--
Norman Diamond diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"It's been a lovely recession."
Technical Details
Message-ID:
<1992Nov13.101813.163@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<15935@goanna.cs.rmit.oz.au>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Subject: Re: fwrite+fread of pointer Message-ID: <1992Nov13.101813.163@nntpd.lkg.dec.com> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <15935@goanna.cs.rmit.oz.au> Date: Fri, 13 Nov 1992 10:18:13 GMT Lines: 40