Question
3. (20 points) Produce an inefficient struct in C that wastes (at least) 77.78% of the space required for the struct. For example, the struct
3. (20 points) Produce an inefficient struct in C that wastes (at least) 77.78% of the space required for the struct.
For example, the struct typedef struct { uint16_t a; uint32_t b; uint16_t c; } S1; takes 12 bytes, but typedef struct { uint16_t a; uint16_t c; uint32_t b; } S2; only requires 8 bytes. Thus it is wasting (12 8) = 4 bytes, or 50% of the space required by the struct.
Ive provided a program on the next page that can be used to compute the inefficiency of struct packing. 1 #include #include typedef struct { uint16_t a; uint32_t b; uint16_t c; } S1; typedef struct { uint16_t a; uint16_t c; uint32_t b; } S2; int main() { double size1 = sizeof(S1); double size2 = sizeof(S2); printf("size: %lu ",sizeof(S1)); printf("size: %lu ",sizeof(S2)); printf("size-inefficiency: %.2f ",100 * (size1 - size2) / size2); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started