S
S.Tobias
pete said:S.Tobias wrote:
Afterthought: the implementation actually need not try to hide it.
It might even allow accessing objects with sub-byte resolution.
But this is implementation defined, and undefined by the standard.
Since void* does not point to any object, is a "free pointer",
the Standard must specify that the void* values which are valid
for char*, are also valid for void*, otherwise undefined. And
this is what a programmer must be concerned about.
I'm still not understanding what you're saying.
The implementation defines byte (char) not what the machine thinks
is a byte (I used name "octet" for machine-bytes). Everything
else is built of bytes.
(I hope my use of the word "octet" is not improper, it wasn't reserved
by the Standard.)
If a char has the same representation as char*,
No, I didn't say that, it's nonsense. All I said is that the
implementation uses machine representation of pointer for char* (and
possibly for other pointer types). Of course, this machine representation
must be composed of multiple of 4-octets (or padded up to that boundary),
so that a pointer type object may be accessed as char array.
and sizeof(char*) is 4, then,
It might be any value. If it's 4, then char* object takes 4 bytes
(or 16 octets, in machine speak).
is each byte of the four bytes of your char*,
also composed of 4 octets?
Of course. On C level everything is built of bytes.
Do you think a pointer to char should have any trouble
stepping through all the bytes of an object of type pointer to char?
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
char *object, *pointer;
size_t n;
n = sizeof object;
object = NULL;
pointer = (char *)&object;
while (n-- != 0) {
printf("byte = 0x%x\n", *(unsigned char *)pointer);
pointer++;
}
return 0;
}
/* END new.c */
I see no problem.
I don't understand what alignment problems
you think that there can be.
pointer = (char*)((unsigned)(char*)&object + 2)
points into the middle of a byte and is not a valid value for a pointer.
Undefined, but for some reason implementation might allow it.
Could there be any conflict with the Standard? (I'm asking, I'm not
completely sure myself.)
Perhaps my use of the word "alignment" was an abuse. I read
now in 3.2 that the Standard defines alignment only with respect
to byte-resolution, where I rather meant sub-byte. I saw a similarity
in that as you can access an object as int for only specific (aligned)
values of int*, in the same manner you can access (in my example)
an object as type char only for specific values of char*.