I know the concept of word boundary whereby computer stores structures using word boundary. I am working on a 64 bit CPU with a 64 bit OS. The value of __WORDSIZE macro in limits.h is 64. So my word boundary is 8 bytes right?
I have two structures:
struct a
{
int a;
char b;
float c;
};
sizeof(struct a) gives 12 as answer. sizeof(int) is 4. sizeof(float) is 4.
For struct b{
char a;
char b;
char c;
char d;
};
sizeof(struct b) is 4.
These outputs suggest the word boundary is 4 bytes. How do I find the word boundary. Is it really equal to sizeof(int) then?
Strangely:
struct c{
char a;
char b;
char c;
char d;
char e;
}
sizeof(struct c) is 5 bytes. Can anyone explain this please.
First of all,
__WORDSIZEis not there for you to inspect. You should never use macros (or other names) beginning with__or_and a capital letter unless they’re specifically documented for your use. It’s an internal part of the implementation used by headers, and the implementation is free to remove or rename it in a later version. Other systems may not even have a corresponding macro at all, or may have it by a different name.With that said, structure alignment has nothing to do with the native “word” size. It has to do with the alignments of the individual elements of the structure.
See my answer to this question:
How does gcc calculate the required space for a structure?