Nominate our 2022 Qt Champions!

[Solved] Help!! Structure is bigger than expected!

  • Hello Qt Community!

    I want to program a simple program that sends raw arp-packets through the network to find out wich ip numbers are in unse and wich not.
    My problem is, that after i compile my project the array 'target_mac' is 2 bytes bigger than i defined it.
    When I define its leght as 6 bytes 2 where add.
    When I define it with a lenght of 5, 3 bytes were add after the array.
    And with a lenght of 4 the array is then 4 bytes big.

    I think I have to add or remove a compiler option, but I have no Idea since I haven't had this problem yet.

    Here is the ARP Srtucture:
    @struct eth_hdr {
    unsigned char dest[6];
    unsigned char src[6];
    unsigned short type;

    typedef struct _arp_hdr {
    struct eth_hdr ethhdr; // Ethernet header
    unsigned short hwtype; // Hardware type
    unsigned short proto; // Protocol type
    unsigned char hlen; // Hardware Address Length
    unsigned char plen; // Protocol Address Length
    unsigned short opcode; // Opcode
    unsigned char sender_mac[6]; // Source hardware address
    unsigned long sender_ip; // Source Ip address
    unsigned char target_mac[6]; // Destination hardware address
    unsigned long target_ip; // Destination Ip address
    } arp_hdr;@

    And here is a Picture of the memory while executing the program.
    The 2 bytes wich were added are marked red. The Green marked Bytes are the char array of the target mac:

    ! Dump)!
    (Link of the Picture: "Memory Dump of ArpPacket-structure": ArpPacket.JPG )

    I use the Qt Version 5.3 with the MinGW 4.8.2 Compiler for 32 Bit.

    Thank you for every help!

  • Which compiler are you used?

  • Sry I have edited my Post.
    I use the Qt Version 5.3 with the MinGW 4.8.2 Compiler for 32 Bit.

  • What about adding @ attribute ((packed))@ to structure?

  • No, that don't solved the Problem..

  • Moderators


    Your compiler padded your struct for data alignment. See for a detailed explanation.

    To disable this, you need to use compiler-specific options. Many compilers use "#pragma pack":

  • Yes, thank u all very much!

    I've now added "# pragma pack (1)" befor the structure definition and a "# pragma pack ( )" after it and it is now working! :)

Log in to reply