Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Produce an inefficient struct in C that wastes (at least) 77.78% of the space re

ID: 3783131 • Letter: P

Question

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; unit32_t b; > S2; only requires 8 bytes. Thus it is wasting (12 - 8) = 4 bytes, or 50%, of the space required by the struct. I've provided a program on the next page that can be used to compute the inefficiency of struct packing. #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: %1u ", sizeof(S2)); printf ("size-inefficiency: %.2f ", 100 * (size1 - size2)/size2); return 0;}

Explanation / Answer

A struct is padded according to the largest type’s alignment requirements . So the largest wastage for c language can happen for a char of 1 byte and a double of 8 bytes . Or if arrays are taken .

Taking regular data types char and double giving inefficiency greater than 77.78 %

typedef struct {

char8_t a ;

double64_t b ;

char8_t c ;

double64_t d ;

char8_t e ;

double64_t f ;

char8_t g ;

double64_t h ;

char8_t i ;

double64_t j ;

char8_t k ;

} S1 ;

typedef struct {

char8_t a ;

char8_t b ;

char8_t c ;

char8_t d ;

char8_t e ;

char8_t f ;

double64_t g ;

double64_t h ;

double64_t i ;

double64_t j ;

double64_t k ;

} S2 ;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote