1

When I ran the following code snippet, got warning, not sure why. The g++ has version 4.8.4.

$ g++ -g  -fPIC -std=c++11 -fpermissive te.cc
te.cc:25:11: warning: ignoring packed attribute because of unpacked non-POD field ‘cMacAddr strArp::src_hw_mac’ [enabled by default]
  cMacAddr src_hw_mac;
           ^
te.cc:27:11: warning: ignoring packed attribute because of unpacked non-POD field ‘cMacAddr strArp::dst_hw_mac’ [enabled by default]
  cMacAddr dst_hw_mac;

Code snippet "te.cc"

#include <iostream>
#include <utility>
#include <functional>
#include <string>

using namespace std;
typedef unsigned char uchar;
typedef unsigned short uint16;
typedef unsigned int uint;

char _ethHdrPrintBuf[100];

class cMacAddr {
public:
    uchar addr[6];
    cMacAddr() { for (int i=0; i<6; i++) addr[i] = 0;}
};

struct strArp {
    uint16 hw_type;
    uint16 proto_type;
    uchar  hw_size;
    uchar  proto_size;
    uint16 opcode;
    cMacAddr src_hw_mac;
    uint  src_proto_ipv4;
    cMacAddr dst_hw_mac;
    uint  dst_proto_ipv4;
} __attribute__((__packed__ )) ;
int main()
{
    cout << sizeof(strArp) << endl;
    return 0;
}
packetie
  • 4,839
  • 8
  • 37
  • 72
  • 2
    It's pretty self explanatory. `cMacAddr` is a "non-POD" field, and is not packed, so the `strArp` is forced to ignore the `__packed__` attribute. – Chad Dec 16 '15 at 03:06
  • @chad: if I drop `__attribute__((__packed__ ))`, I don't see the warning, however, the structure strArp is 4 bytes bigger. – packetie Dec 16 '15 at 03:17

1 Answers1

0

cMacAddr has a constructor, therefore it can't be a POD. Too much detail here.

Community
  • 1
  • 1
eh9
  • 7,340
  • 20
  • 43
  • thanks for the link. But having `__attribute__((__packed__ ))` give me the desired result in terms of size of the struct. How do I remove the warning then? – packetie Dec 16 '15 at 03:18
  • 1
    Either remove the constructor from `cMacAddr`, or give that the `__packed__` attribute as well. – Chad Dec 16 '15 at 03:20
  • If it were me, I'd declare with a POD class for `cMacAddr`. You can zero out the structure in some way other than the constructor. – eh9 Dec 16 '15 at 03:21