F
Francis Moreau
Hello,
For optimisation purposes, I'd like to use some unused bits of a
pointer. This assumes a certain bits layout for pointer encoding hence
making the code not portable.
These pointers are pointers to a structure 'A':
struct A {
unsigned long a;
};
so I assume that their alignment requirement should be the same as the
first member whose type is 'unsigned long'.
Now I don't know from the C specification what is the alignment
requirement for 'unsigned long' type (I assume it's unspecified, but
not sure). From my experience, I always find them to align to either 4
bytes or 8 bytes boundaries. So this leaves a couple of unused bits in
the pointer which I'd like to use.
For example, I'd like to use one of these bit when condition 'C1' is
true:
struct A *p;
/* setup 'p' */
if (C1)
*p = (struct A *)((uintptr_t)p | 1);
(In this example I used 'uintptr_t' type which is 'optional', so this
probably means that using this type implies non portable code.)
So the question is: even the code above is not portable, it seems to
me that it should work on most systems (I'm aware), is this correct ?
Thanks
For optimisation purposes, I'd like to use some unused bits of a
pointer. This assumes a certain bits layout for pointer encoding hence
making the code not portable.
These pointers are pointers to a structure 'A':
struct A {
unsigned long a;
};
so I assume that their alignment requirement should be the same as the
first member whose type is 'unsigned long'.
Now I don't know from the C specification what is the alignment
requirement for 'unsigned long' type (I assume it's unspecified, but
not sure). From my experience, I always find them to align to either 4
bytes or 8 bytes boundaries. So this leaves a couple of unused bits in
the pointer which I'd like to use.
For example, I'd like to use one of these bit when condition 'C1' is
true:
struct A *p;
/* setup 'p' */
if (C1)
*p = (struct A *)((uintptr_t)p | 1);
(In this example I used 'uintptr_t' type which is 'optional', so this
probably means that using this type implies non portable code.)
So the question is: even the code above is not portable, it seems to
me that it should work on most systems (I'm aware), is this correct ?
Thanks