Reputation: 2580
I read something like pointer must be byte-aligned. My understanding in a typical 32bit architecture... all pointers are byte aligned...No ?
Please confirm.
can there be a pointer which is not byte-aligned ?
Basically this is mentioned in a hardware reference manual for tx descriptor memory.
Upvotes: 2
Views: 446
Reputation: 49311
In C, a pointer points to an object†
The only objects which are not a whole number of bytes are bit fields.
The C language does not allow you to create a pointer to a bit field; this code will result in a compiler error: "cannot take address of bit-field ‘b’":
struct S { unsigned int a:4, b:4, c:4, d:3, e:1; };
int main ( void ) {
struct S s;
int *i = &s.b; // would point half a byte into s
return 0;
}
Pointers can only be incremented by a whole number of the size of object they point to.
Since you can't create such a pointer to an object of size less than one byte, or increment a pointer by less than one byte, you cannot have a pointer which is less than one byte aligned.
† in the C sense, not the OO sense
Upvotes: 5
Reputation: 75598
If a pointer or a number is not byte aligned, it would start in the middle of a byte. I.e. some bits of a byte would belong to one pointer, and other bits to another. This would be strange and it does not occur in practice.
Upvotes: 1