那些年遇到的坑不可重入函数inet_ntoa

那些年遇到的坑不可重入函数inet_ntoa

inet_ntoa–不可重入函数

char *inet_ntoa(struct in_addr in);

The string is returned in a statically allocated buffer, which subsequent calls will overwrite.

    unsigned long n1 =  inet_addr("10.213.120.1");
	unsigned long n2 =  inet_addr("172.18.18.1");
	
	struct in_addr stAddr1, stAddr2;
	stAddr1.s_addr = n1;
	stAddr2.s_addr = n2;
 
    printf("p1 %s p2 %s\n", inet_ntoa(stAddr1), inet_ntoa(stAddr2));

结果是啥呢:

p1 10.213.120.1 p2 10.213.120.1

显而易见printf 从右往左压栈时stAddr1将stAddr2的结果覆盖了!

猜你喜欢

转载自blog.csdn.net/vegeta852/article/details/109494210