C ++ - Initializing SOCKADDR_IN

I work through some defects of static analysis, and this causes me a problem.

SOCKADDR_IN m_stLclAddr; 

SOCKADDR_IN is a member of the WinSock API

Defect says that I did not initialize the following:

  • m_stLclAddr.sin_port
  • m_stLclAddr.sin_zero
  • m_stLclAddr.sin_addr
  • m_stLclAddr.sin_family

I am not very familiar with the WinSock API, but I did a little research, and I just want to know if the next line of code will initialize m_stLclAddr with default values โ€‹โ€‹?:

 m_stLclAddr = { 0 }; 
+7
source share
2 answers

m_stLclAddr = {0} will set everything to zero for the first time (optionally default values โ€‹โ€‹or what you really want to do). memset(&m_stLclAddr, 0, sizeof(SOCKADDR_IN)); set everything in m_stLclAddr to zero for not only initialization, but also subsequent calls.

I would think that you want to do something like this:

 local_sin.sin_family = AF_INET; local_sin.sin_port = htons (PORTNUM); local_sin.sin_addr.s_addr = htonl (INADDR_ANY); 

as shown here: http://msdn.microsoft.com/en-us/library/aa454002.aspx

+4
source

Yes, with {0} m_stLclAddr will be initialized to all zero

+1
source

All Articles