In article <news:2r******* ******@uni-berlin.de>
S.Tobias <sN*******@amu. edu.pl> wrote:
[much snippage]
The last manner is also found in: 6.3.2.3#7 (Pointers) and 7.20.3#1
(Memory management functions). See for yourself: in both cases "pointer
alignment" refers to the pointer *value*, not pointer type.
I am not going to address most of this (due to lack of time), but
I want to make several points here.
First, the value of a pointer to some type "T" -- i.e., a value of
type "T *" -- indeed possesses this "alignment" characteristic, so
that it is possible to ask whether such a pointer is correctly
aligned. (This question is inherently machine-dependent, however,
and on *some* machines it is meaningless.)
But note that if we store this pointer value in an object:
T *pointer_object ;
we then have an *object*, not a variable. This object can have an
address:
T **p2 = &pointer_object ;
(here the object's address is stored in yet another object, "p2",
of type "T **). If "pointer_object " has an address, that value --
a value of type "T **" -- *also* possesses this "alignment"
characteristic. Thus, we can ask not only whether the value stored
in "pointer_object " is correctly aligned, but also whether
"pointer_object " itself is correctly aligned. Having stored
&pointer_obj ect in p2, we can then ask whether "the object p2" is
correctly aligned, via this question:
"does &p2 satisfy its machine-dependent alignment constraints?"
and if we store &p2 in yet another object:
T ***p3 = &p2;
we can then go on to ask whether "the object p3" (or more precisely,
the value produced by &p3) is correctly aligned, and so on.
There are thus four possible machine-dependent alignment constraints
arising so far:
(1) the value stored in pointer_object (of type "T *")
(2) the value produced by &pointer_obj ect (type "T **")
(3) the value produced by &p2 (type "T ***")
(3) the value produced by &p3 (type "T ****")
and it makes some sense to ask whether all of them are satisified,
and to ask whether any or all four of these machine-dependent
alignment constraints are identical. Some of the answers are of
course machine-dependent. Since the objects named "pointer_object ",
"p2", and "p3" are allocated by the compiler, they had better be
correctly aligned -- the C programmer is not the one responsible
for this -- but the rest of the answers require looking at the
machine.
For a number of existing, even common, machines today, the alignment
constraints for "T *" and "T **" often differ, depending on T.
For instance, "char *" always, by definition, needs only 1-byte
alignment ("C byte", not necessarily 8-bit-octet, although the
machines I am thinking of happen to have 8-bit C bytes too); but
"char **" often requires 4 or 8 byte alignment (32-bit or 64-bit
SPARC and MIPS, for instance).
Now, given the original example, or something at least close to
it:
struct C { char c; };
struct D { double d; };
you will find that, e.g., SPARC systems have sizeof(struct C) == 1
and align "struct C" objects on byte boundaries, while sizeof(struct D)
== 8 and "struct D" objects are always found on 8-byte boundaries.
Nonetheless, on those machines in 32-bit mode (or always for V8 and
earlier SPARC systems), "struct C *" and "struct D *" objects
are always aligned on 4-byte boundaries. In other words, given:
struct C *p1, **p2;
... set p1 and p2 ...
we can test the "alignment validity" (on that SPARC machine, in a
machine-dependent manner) with:
int is_p1_aligned(v oid) {
/* p1 is always properly aligned */
return 1;
}
int is_p2_aligned(v oid) {
/* p2 is properly aligned if and only if its low 2 bits are 0 */
return ((int)p2 & 3) == 0;
}
If we add "struct D *p3", however, the "is_p3_alig ned" test becomes:
int is_p3_aligned(v oid) {
/* p3 is properly aligned if and only if its low 3 bits are 0 */
return ((int)p3 & 7) == 0;
}
Thus, the "aligned-ness" of a pointer depends on its type as well as
its value; and for two "struct T1 *" and "struct T2 *" values, the
"aligned-ness" of those two value may require different tests.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.