Consider a C structure Object that includes the following me
Consider a C structure (Object) that includes the following members. Assume a 64-bit architecture with 64-bit memory addresses: struct Object {bool b; double d; short sl; char * cptr;//pointer to char short s2; short s3; float * fptr;//pointer to float float f; int i;}; If the compiler does not align data in memory, what is the minimum size of the struct Object in bytes? If the compiler aligns data in memory according to their size, but does not reorder the members of the structure, what will be the size of the struct Object? How many bytes are wasted internally inside the structure due to memory alignment? If the compiler aligns data in memory and can also reorder the members of the structure, how the members of the struct Object should be reordered? What will be the size of the struct Object?
Solution
a) 52 bytes
b) 52 bytes, and 5 bytes are wasted. for boolean (3 bytes) and first short(2 bytes)
c) Keep double first and boolean second i.e.
double d;
bool b;
short s1;
char *cptr;
short s2;
short s3;
float *fptr;
float f;
int i;
Size is 48 bytes
