Padding and the size of an allocated array of structs

A logger allocates its buffer of readings with calloc(50, sizeof(struct sample)). Two versions of the structure are being compared:


C
struct sample {          /* original order */
    char   tag;
    double value;
    int    count;
};

struct sample {          /* reordered */
    double value;
    int    count;
    char   tag;
};


Assume a typical 64-bit Linux platform: char is 1 byte with alignment 1, int is 4 bytes with alignment 4, and double is 8 bytes with alignment 8. The compiler places members in declaration order and adds only the padding that alignment requires, including trailing padding so that sizeof is a multiple of the struct's largest member alignment.


Fill in each member's byte offset, the value of sizeof(struct sample), and the total bytes requested by the calloc call, as integers, for both versions.

Quantity (bytes)Original orderReordered
Offset of tag
Offset of value
Offset of count
sizeof struct sample
Total requested for 50 samples

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Dynamic UDTs Questions: