Article View: comp.std.c
Article #2946Re: fwrite+fread of pointer
From: Richard A. O'Keefe
Date: Mon, 16 Nov 1992 09:33
Date: Mon, 16 Nov 1992 09:33
41 lines
2613 bytes
2613 bytes
In article <1992Nov15.065204.22137@sq.sq.com>, msb@sq.sq.com (Mark Brader) writes:
> First, I take it that everyone agrees that the bytes read back from
> the file have to be the same ones that were written out. The question
> then is whether the bytes that represent the pointer value &dummy have
> to always be the same ones, during the life of the variable "dummy".
That is indeed the question. For the benefit of some comp.std.c readers
who have mistaken me for a novice who _wants_ to do this kind of thing,
I'm not. I have been arguing for a while in comp.lang.c that it would be
a good thing if C compilers were to warn about fwrite(&x... or fread(&x...
where x is of a type which can include pointers. I have seen too many
students get this wrong. It is a particularly easy mistake to make:
struct Tag { ... char field[20]; ... } x;
fwrite(&x, sizeof x, 1, f);
and then the instructor points out that `field' is supposed to hold
file names and 20 is absurdly small, so the student changes it to
struct Tag { ... char *field; ... } x;
and misses a couple of the fwrite()s. I received mail from several people
defending the fact that C compilers don't point out the problem, all of
them taking the line that it _might_ be useful, though none of them cited
an actual working program they had written or even read. The question then
arose: is it EVER possible for a _portable_ program to write out a pointer
and usefully read it back?
It was always clear that you should get the same _bytes_ back, but it was
not clear to me that it has to be the same _pointer_. Mark Brader has
understood the question perfectly.
> My opinion is that they do; this opinion is based, somewhat shakily,
> on the definition of an object as "a region of data space in the
> execution environment", where the key word is "a".
The question can now be paraphrased: is a pointer like a tensor, or like
the components of a tensor? I would regard a compacting garbage collector
as simply changing the co\"ordinate system. I have used hardware and
software systems where stacks could be moved (in order to "stretch" them)
while the owning thread was running.
Technical Details
Message-ID:
<15976@goanna.cs.rmit.oz.au>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!goanna!okReferences:
<15935@goanna.cs.rmit.oz.au> <1992Nov13.101813.163@nntpd.lkg.dec.com> <1992Nov15.065204.22137@sq.sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.std.c Subject: Re: fwrite+fread of pointer Message-ID: <15976@goanna.cs.rmit.oz.au> Date: 16 Nov 92 09:33:15 GMT References: <15935@goanna.cs.rmit.oz.au> <1992Nov13.101813.163@nntpd.lkg.dec.com> <1992Nov15.065204.22137@sq.sq.com> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 42