I have the following structure.
typedef struct {
int8_t tmsi[4]; /**< TMSI value. */
int8_t ptmsi[4]; /**< PTMSI value. */
int8_t gprs_attach_status;
int8_t rplmn[3]; /**< PLMN info */
uint32_t T3212_value;
uint32_t T3312_value;
uint8_t cs_reject_cause;
uint8_t ps_reject_cause;
int8_t qos[28]; /** QoS parameters for a PDP context. */
int8_t pdp_addr_len;
int8_t pdp_address[10];
uint16_t apn_addr_len; /**< APN address length. */
int8_t apn_address[20]; /**< APN address. */
}nas_ftd_umts_nas_info_s_type_v01 ;
The size of this structure without padding should be 83.But when compiled on 64 bit processor the size is showing as 84. Compiler is allocation one extra byte for pdp_address[10].Not sure why this extra byte is allocated. Can anyone let me know the reason for this?
I checked the offset of each member with the following code:
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
int main()
{
temp a;
test_ping_req_msg_v01 t;
nas_ftd_umts_nas_info_s_type_v01 info;
nas_ftd_umts_network_info_s_type_v01 lte;
nas_umts_ftd_info_ind_msg_v01 ftd;
cout << sizeof(info) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, ptmsi) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, gprs_attach_status) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, rplmn) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, T3212_value) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, T3312_value) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, cs_reject_cause) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, ps_reject_cause) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, qos) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, pdp_addr_len) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, pdp_address) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, apn_addr_len) << endl;
cout << OFFSETOF(nas_ftd_umts_nas_info_s_type_v01, apn_address) << endl;
}
output :
yamunarani@yamunarani:~$ ./a.out
84
4
8
9
12
16
20
21
22
50
51
62
64
n/CHAR_BITbytes). The same goes for every element of an array. In your case, every element ofpdp_address(andpdp_addressitself) will be aligned on a 16-bit boundary, and every second element ofpdp_addresswill be aligned on a 32-bit boundary (since 32 is twice 16). Ifpdp_address[9]is on a 32-bit boundary, the only wayapn_addr_lencan be on a 32-bit boundary is if there is padding between them#pragma pack(1)or equivalent, depending on the compiler you are using.