When zeroing a struct such as sockaddr_in, sockaddr_in6 and addrinfo before use, which is correct: memset, an initializer or either?

后端 未结 5 1704
执笔经年
执笔经年 2020-12-14 17:36

Whenever I look at real code or example socket code in books, man pages and websites, I almost always see something like:

struct sockaddr_in foo;
memset(&         


        
5条回答
  •  被撕碎了的回忆
    2020-12-14 17:51

    "struct sockaddr_in foo = { 0 };" is only valid for the first time, whereas "memset(&foo, 0, sizeof foo);" will clear it each time the function is run.

提交回复
热议问题