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

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

 



When compiling the following code with GCC 4.4.0 on Solaris 10/Sparc with the -Wcast-align flag, I get a warning:


#include <stdio.h>
#include <netinet/in.h>

int main() {
    struct sockaddr wtf;
    struct sockaddr* w = &wtf;
    struct sockaddr_in* w2 = (struct sockaddr_in*) w;
    printf("%p\n", w2);
    return 0;
}


test.cc: In function ‘int main()’:
test.cc:7: warning: cast from ‘sockaddr*’ to ‘sockaddr_in*’ increases required alignment of target type

It is easy enough to work around this warning by assigning to void* first. However, why does GCC think that struct sockaddr and struct sockaddr_in have different alignment requirements? They are both 16 bytes long. If you create an array of the two types, the elements are packed without any padding. Thus, it seems like GCC thinks they have the *same* alignment requirements in arrays, but different requirements in pointers? I don't get it. Is this a bug?

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