I have defined in my C program a struct
as shown below:
typedef struct {
long time_of_arrival; // Number of minutes after the salon opening when the customer arrived
char customer_name[21]; // Upto 20 characters long in all caps
char stylist_name[21]; // Preferred stylist name, upto 20 characters long or NONE, in all caps
long loyalty_points; // The loyalty points customer holds
long requested_haircut_time; // The time corresponding to the haircut customer requested
long loyalty_points_earned; // The loyalty points customer earned
} customer;
I wish to create an array of structures of size per the number of customers as supplied by the user via command line argument.
To determine the amount of memory a single instance of the said structure would occupy in memory, I ran the following snippet of code:
int main(int argc, char const *argv[]) {
printf("%ld\n", sizeof(customer));
return 0;
}
which printed the following output:
80
However, in my limited understanding, the size should be 74 bytes, i.e. 8 bytes for each long
variable and 21 x 2 bytes for the two character arrays.
Upon further debugging, (i.e. by creating a standalone array of 21 characters and checking its size using sizeof
operator, which got reported as 21 bytes), I concluded that for some reason, the size of character array under the structure was getting rounded up to the multiple of 8 bytes.
I need help in understanding what phenomenon is responsible for this behaviour and what is it that I am missing here?
Note: I am building the C program using Apple clang version 13.0.0 (clang-1300.0.29.3) running on macOS 12.0.1 (21A559).