1

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
2
  • 1
    An n-bit integer (typically) needs to be aligned on a n-bit boundary, where n is 8, 16, 32, etc. Roughly speaking, that means the address of a n-bit integer is a multiple of n bits (or n/CHAR_BIT bytes). The same goes for every element of an array. In your case, every element of pdp_address (and pdp_address itself) will be aligned on a 16-bit boundary, and every second element of pdp_address will be aligned on a 32-bit boundary (since 32 is twice 16). If pdp_address[9] is on a 32-bit boundary, the only way apn_addr_len can be on a 32-bit boundary is if there is padding between them Commented Oct 19, 2019 at 0:04
  • If you want to eliminate padding between fields, force the structure to use 1-byte alignment via #pragma pack(1) or equivalent, depending on the compiler you are using. Commented Oct 19, 2019 at 4:14

1 Answer 1

6

The alignment requirement of uint16_t is that it starts on an even address. Without padding of pdp_address[10], you have 61 bytes (i.e. odd) until apn_addr_len, so the compiler pads pdp_address with one byte to get apn_addr_len onto an even address.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.