News for hackers. Stuff that matters.
Re:Show HN: Minecraft clone in 2500 lines of C - even suppor... by jburgueno on Tuesday December 10, 2013 @09:27AM (#6880860)
client.c: 145

struct sockaddr_in address; ... memset(&address, 0, sizeof(address));

Can someone explain me why you need to set memory of sockaddr_in to 0 ? (Even if &address haven't been touched).

Re:Show HN: Minecraft clone in 2500 lines of C - even suppor... by anywherenotes on Tuesday December 10, 2013 @11:00AM (#6881413)
it's a local variable, and local variables do not get initialized in C.
Re:Show HN: Minecraft clone in 2500 lines of C - even suppor... by jburgueno on Tuesday December 10, 2013 @01:39PM (#6882651)
But the initialization of the variable is right after the memset:

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = ((struct in_addr *)(host->h_addr_list[0]))->s_addr;
    address.sin_port = htons(port);
Why is it useful the put 0 before setting those values?
Re:Show HN: Minecraft clone in 2500 lines of C - even suppor... by oh_teh_meows on Tuesday December 10, 2013 @04:09PM (#6883711)
It's good practice in general. There might be other fields that were not explicitly set, or the struct may expand in future versions, or you have union fields/bitfields (e.g. you explicitly set the low order bits of some union field to some value, thinking the high order bits are all zeroes, but because you neglected to zero them out first, they're filled with garbage bits. when you read the union field as a whole you'll get incorrect values).
Re:Show HN: Minecraft clone in 2500 lines of C - even suppor... by jburgueno on Thursday December 12, 2013 @12:57PM (#6895877)
Thanks for the explanation!