Is there any final guide that says we should initialize the sockaddr_in structure to zero for a reason?
// IPv4 AF_INET sockets: struct sockaddr_in { short sin_family; // eg AF_INET, AF_INET6 unsigned short sin_port; // eg 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 at real code or an example or books, it always has a 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 say that the element char sin_zero[8] exists for filling and should be set to zero, but why reset the rest. Especially when they are initialized in most cases in the next few lines of the declaration. Even about the sin_zero member in the programming guide, beej said it was not necessary to reset them anymore . I was looking for an explanation, but nothing worked. This stack overflow issue has several different suggestions for initialization itself, but does not explain why it is necessary to reset.
Is this an outdated practice, or is there some real reason I'm missing out on? Any link is appreciated.
c ++ sockets network-programming memset
jazaman
source share