Is there any definitive guide that says we have to initialize the sockaddr_in struct to zero for a particular reason?
// IPv4 AF_INET sockets:
struct sockaddr_in {
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};
Whenever I looked in real code or example or books it always has the code structure similar to the following:
struct sockaddr_in foo;
memset(&foo, 0, sizeof(foo));
foo.sin_port = htons(1025);
foo.sin_family = AF_INET;
inet_pton(AF_INET, "10.0.0.1", &foo.sin_addr);
I understand that some sources says that the char sin_zero[8]
member is there for padding and should be set to zero but why zeroing the rest. Especially when they are initialized in most cases within next few lines of declaration. Even about sin_zero member the beej programming guide said it is not mandatory to zero them out anymore. I have searched for an explanation but nothing to the point turned up. This stack overflow question has some different suggestions on the initialization itself but does not explain why the zeroing is necessary.
Is it a legacy practice or there are some real reason I am missing out? Any reference is appreciated.