Re: -Wcast-align and struct sockaddr_in / struct sockaddr: why the warning?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ian Lance Taylor wrote:
The size of the type does not describe the required alignment of the
type.  If, say, struct sockaddr is defined as { char x[16]; } then the
size is 16 but it may be aligned on any 8-bit boundary.  If, say, struct
sockaddr_in has fields of type short or int, then the size may be 16 but
it may require alignment on a 16-bit or 32-bit boundary.

Whoops, I should have figured that out. Thanks for the detailed answer. I guess I'm just annoyed that you can't use the sockets API without producing these warnings. Good thing the "assign to void*" workaround exists.


For reference, this issue occurred when compiling for Solaris/Sparc, where the definitions seem to be:


struct sockaddr {
        sa_family_t     sa_family;      /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};


struct sockaddr_in {
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        unsigned char   sin_zero[8];
};


sa_family_t = uint16_t;
in_port_t = uint16_t
struct in_addr = gross union of stuff; 32-bits wide


So I guess I understand this now: sockaddr probably needs 16-bit alignment (due to sa_family), whereas sockaddr_in needs 32-bit alignment (due to sin_addr).

Thanks for your help,

Evan Jones

--
Evan Jones
http://evanjones.ca/

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux