Calculate the size in bytes of the following structure:
struct{
int b;
char a;
int c;
}
Calculate the size in bytes of the following structure?
It depends...
An int is the natural size of data bus of the processor. So depending on the processor it could be 2 bytes or 4 bytes. Lets assume that you are talking about a 32 bit processor, then an int would be 4 bytes. A character is only 1 byte. But here's the catch. Most processors want to align integers on word boundaries, so the int 'b' is 4 bytes, and the character 'a' would also take 4 bytes even though only 1 byte is used. The other 3 bytes are padding. Following the character 'a' is another int (4 bytes) which must be aligned on the next word boundary.
So, your structure would take 12 bytes. You can use structures more efficiently by placing character variables together, so as to minimize the wasted space.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment