Nonsense, the size is the size of the data pointed to.
Is it now. The size in what units? void has no size. So how do we
know the size of the units a void * points to? Is that size the number
of int's? Of struct foo's?
the length is a length in bytes.
Ah, always in bytes you say. Excellent. Now we know the implicit size
of void.
int n;
write( &n, sizeof(n) ); ....
So nonsense like:
int n;
int *p = n;
(I think you mean &n there.)
...
void* v = p;
++v;
would become legal?
Why not? How is that different from what write(&n, sizeof(n)) has to
do with the &n in your example? Inside write(), it must consider &n to
point to a sequence of bytes, not to an integer and not to a mystery.
Therefore we have fallen from the purer faith by conceding that void *v
points to a sequence of bytes. Having already sinned, ++v must mean
point to the next byte. write() could be something like:
extern void writeonebyte(void *);
void write(void *p, size_t n)
{
while (n) {
writeonebyte(p);
p++;
n--;
}
}
That's all entirely consistent with the C standard library
interpretation of void * as a pointer to a sequence of bytes.
Anyway, I like Keith's proposal better, which is to have a "byte" type
which would combine the type behavior of char (or better yet unsigned
char) with the casting behavior of void. Then functions that take
arbitrary chunks of memory would use byte * instead of void *, and
everything would make sense. void * would remain immune to arithmetic,
and would be used when you mean an obscured object as opposed to a
sequence of bytes. When you mean sequences of bytes, like for
memcpy(), you would then use byte *. But you wouldn't need to cast the
pointers to byte *, so you could in fact say memcpy(dest, &n,
sizeof(n)) as opposed to memcpy(dest, (byte *)(&n), sizeof(n)).
I guess part of the problem is that void is a bit overloaded in C,
meaning a bunch of rather different things depending on the context: no
arguments (int main(void)), doesn't return anything (void func(int)),
deliberately dispose of the return value ((void)fclose(f)), obscured
data object (void *foo), and sequence of bytes (memcpy(..., void *,
size_t)). The last two are in a spritual conflict.
Anyway, this has been enjoyable and instructive for me, so thank you
all for your time. Some people seem to get a little ruffled in these
religious discussions, so I hope I didn't offend anybody. So as to not
waste any more of your time, I'll leave you alone and go back to
working on the next release of zlib, which now has two occurences of
buf = (char *)buf + n.
Mark