title: Unix socket: hostent makes memory leaks
body: <p>I am writing client for TCP connection and conversion from IP to socket_addr makes memory leaks.</p>
  
  <p>There is following process:</p>
  
  <pre><code>#include &lt;netdb.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  
  /** there is some code like method header etc. */
  
  hostent * host = gethostbyaddr( ip, 4, AF_INET ); // ip is char[4], I use IPv4
  
  if ( !host ) return -2; // bad IP
  
  netSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  if ( netSocket == -1 ) return -3; // error during socket opening
  
  sockaddr_in serverSock;
  serverSock.sin_family = AF_INET;
  serverSock.sin_port = htons( port );
  memcpy( &amp;( serverSock.sin_addr ), host-&gt;h_addr, host-&gt;h_length );
  
  // and now there is function connect(...);
  
  /** end of method */
  </code></pre>
  
  <p>This code works fine but when I tracked memory using I took 5 memory leaks. They are created by this line:</p>
  
  <pre><code>hostent * host = gethostbyaddr( ip, 4, AF_INET ); // ip is char[4], I use IPv4
  </code></pre>
  
  <p>I have tried delete it <code>delete host;</code> but this causes segmentation fault.</p>
  
  <p>Do you have any ideas how I can clean the memory, please? This is my school project and we have to work with memory correctly.</p>
  
  <p>EDIT:
  I am using Linux Ubuntu 9.04, g++ 4.3.3 and for memory testing mudflap library</p>