J
James Harris \(es\)
Am I right that there is no guarantee in C that pointers to different types
can be stored in one another except that a pointer of any type can be stored
in a void * and later recovered without loss of info?
What is the rationale for distinguishing the convertability of pointers
based on the type of the object they point to? Are there machines on which a
pointer to a char would have different *size* from a pointer to a float,
say, or that a pointer representation might be changed when converted? I can
imagine that the two addresses may need different alignments (perhaps floats
needing 4-byte alignment, chars needing only 1-byte alignment). This would
mean that the rules for the *lower* bits of a pointer could be different.
This would then be nothing to do with pointer size. The rule about void
pointers would simply mean that void pointers had no alignment restrictions.
Is that the reason why void pointers can be used in a special way?
On some computers it makes sense to distinguish a pointer's size based not
on the type of the object pointed at but on *where* that object could be
stored. On old hardware such as 16-bit x86 there were near or far pointers.
On modern multi-machine clusters it might make sense to allow pointers to
local memory to be short while pointers to remote memory are longer. On old
and new hardware, then, it's not the type of the object pointed at but the
location of the object pointed at which would determine the requirement for
pointer size.
Some compilers allow the user to specify that all pointers will be short or
all pointers will be long - e.g. the awful old memory models. Wouldn't it be
better for a compiler to choose pointer sizes depending on where the object
to be referred to might be placed? Basically, all pointers would default to
long but where the compiler could prove that a given pointer can be used for
only local references that pointer could be made short.
James
can be stored in one another except that a pointer of any type can be stored
in a void * and later recovered without loss of info?
What is the rationale for distinguishing the convertability of pointers
based on the type of the object they point to? Are there machines on which a
pointer to a char would have different *size* from a pointer to a float,
say, or that a pointer representation might be changed when converted? I can
imagine that the two addresses may need different alignments (perhaps floats
needing 4-byte alignment, chars needing only 1-byte alignment). This would
mean that the rules for the *lower* bits of a pointer could be different.
This would then be nothing to do with pointer size. The rule about void
pointers would simply mean that void pointers had no alignment restrictions.
Is that the reason why void pointers can be used in a special way?
On some computers it makes sense to distinguish a pointer's size based not
on the type of the object pointed at but on *where* that object could be
stored. On old hardware such as 16-bit x86 there were near or far pointers.
On modern multi-machine clusters it might make sense to allow pointers to
local memory to be short while pointers to remote memory are longer. On old
and new hardware, then, it's not the type of the object pointed at but the
location of the object pointed at which would determine the requirement for
pointer size.
Some compilers allow the user to specify that all pointers will be short or
all pointers will be long - e.g. the awful old memory models. Wouldn't it be
better for a compiler to choose pointer sizes depending on where the object
to be referred to might be placed? Basically, all pointers would default to
long but where the compiler could prove that a given pointer can be used for
only local references that pointer could be made short.
James