struct hostent FAR *gethostbyname(
  const char FAR *name  
);返回的指针当我用完之后是否要delete

解决方案 »

  1.   

    NO, 这个函数明确指出, 返回的指针不能DELETE ,此内存为系统所有
      

  2.   

    不需要deleteThe gethostbyname function returns a pointer to a hostent structure—a structure allocated by Windows Sockets. The hostent structure contains the results of a successful search for the host specified in the name parameter.The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.
      

  3.   

    呵呵, 楼上说得很详细. 跟我在 MSDN  上看到的差不多.这个函数返回的指针指向一个缓冲区,是属于WINSOCK 所有的. 每个线程有都一个.你不能删除它. 并且如果你需要这些信息在调用以后. 应该将它拷贝一份出来,而不是保存此指针. 因为它指向的区域可能会另有它有. 以前有一次搞笑的经历. 我需要将两个IP地址写入数据库. 一个起始IP一个终止IP. 因为是其它主机传过来的,所以为了减少消息长度,传的是 u_long 于是这样格式化 SQL:char sql [1024];sprintf(sql, "INSERT INTO tb_iplimit(startip, endip, [time], adminid) VALUES('%s','%s',getdate(), '%s')", inet_ntoa(*(in_addr*)&startip), inet_ntoa(*(in_addr*)&endip), admin.c_str());_conn.Execute(sql, ...);
    结果每次写入的 起始 IP ,和结束 IP 都是一模一样的. 困扰了我好久..... 汗!!
      

  4.   

    gethostbyname 函数如果调用成功,系统就会返回一个指向H O S T E N T结构的指针。注意,保存H O S T E N T结构的是系统内存。应用程序不应该依靠它来维护状态。由于该内存由系统维护,因此,你的应用程序不必释放这个已返回的结构。
      

  5.   

    I was late, and your guys had replied the answers.