Recently wrote a program that roughly uses the following code:
Wsadata Wsadata; WSAStartup (Makeword (2,2),&Wsadata);struct addrinfo *ailist=0 char *IP; whileif (0==getaddrinfo ( "www.qq.com 0,0,&ailist) {addr=* (struct sockaddr_in *) ailist->ai_ Addr Ip=inet_ntoa (ADDR.SIN_ADDR);} //
After many cycles, it is found that Inet_ntoa destroys the stack space. After searching, the string returned by Inet_ntoa is temporarily loaded in a statically allocated buffer, and the buffer is rewritten the next time this function is called.
http://blog.csdn.net/litingli/article/details/5461535
Msdn:
The string returned by Inet_ntoa resides in memory which is allocated by Windows Sockets. The application should not make any assumptions about the the-the-which the memory is allocated. The string returned is guaranteed to being valid only until the next Windows Sockets function call is made within the same th Read. Therefore, the data should be copied before another Windows Sockets call is made.
But still don't know why the stack is broken, overwriting the local variables.
Solution:
Wsadata Wsadata; WSAStartup (Makeword (2,2),&Wsadata);struct addrinfo *ailist=0 in char buffer[32char *bytes = (unsigned char *) &insizeof (buffer), "%d.%d.%d.%d" ,bytes [0], Bytes[1], Bytes[2 ], Bytes[3]);