在GNU C Library Reference里函数inet_ntoa有如下描述:
char * inet_ntoa (struct in_addr addr)
This function converts the IPv4 Internet host address addr to a string in the standard numbers-and-dots notation. The return value is a pointer into a statically-allocated buffer. Subsequent calls will overwrite the same buffer, so you should copy the string if you need to save it. In multi-threaded programs each thread has an own statically-allocated buffer. But still subsequent calls of inet_ntoa in the same thread will overwrite the result of the last call.
注意粗体部分,inet_ntoa函数返回的指针系指向一个固定分配的缓存区的,因此每次调用inet_ntoa都会覆盖上次执行的结果,用来直接输出就没什么问题,但如果要保存的话,就需要自己另外开辟空间将结果复制过去了~~另外对于多线程程序,由于每个线程都有其独立的固定分配缓存区,因此各个线程之间不会互相覆盖对方的执行结果。
0 意見