Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Help!! Structure is bigger than expected!
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave007
    wrote on last edited by
    #1

    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:

    !http://www.mediafire.com/view/cvoqe6ck6ik9u4j/ArpPacket.JPG(Memory Dump)!
    (Link of the Picture: "Memory Dump of ArpPacket-structure":http://www.mediafire.com/view/cvoqe6ck6ik9u4j ArpPacket.JPG )

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

    Thank you for every help!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexwolf
      wrote on last edited by
      #2

      Which compiler are you used?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dave007
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexwolf
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dave007
            wrote on last edited by
            #5

            No, that don't solved the Problem..

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              Hi,

              Your compiler padded your struct for data alignment. See http://msdn.microsoft.com/en-us/library/ms253949(VS.80).aspx for a detailed explanation.

              To disable this, you need to use compiler-specific options. Many compilers use "#pragma pack":http://www.cplusplus.com/forum/general/14659/

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dave007
                wrote on last edited by
                #7

                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! :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved