Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Initializing SOCKADDR_IN

I am working through some Static Analysis defects and one that is causing me an issue is this one.

SOCKADDR_IN m_stLclAddr;

SOCKADDR_IN is a member of the WinSock API

The defect is saying that I have not initialized the following:

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

I am not very familiar familiar with the WinSock API but I have done a bit of research and I just want to know if the following line of code would initialize m_stLclAddr with default values?:

m_stLclAddr = { 0 };
like image 987
DukeOfMarmalade Avatar asked Jun 06 '12 16:06

DukeOfMarmalade


1 Answers

m_stLclAddr = {0} will set everything to zero the first time (not necessarily default values or what you actually want to do). memset(&m_stLclAddr, 0, sizeof(SOCKADDR_IN)); will set everything in m_stLclAddr to zero for not only initialization, but successive calls as well.

I would think you would 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

like image 129
bn. Avatar answered Oct 23 '22 15:10

bn.