c 语言套接字编程怪事件

2019-09-25 22:06:06 +08:00
 b00tyhunt3r
int main()

{
  struct addrinfo* c;   //创建套接字结构
  char host[1024],serv[1024];
  getaddrinfo("163.com","http",NULL,&c); //取得猪厂 ip 地址及 http 端口号
  int flags = NI_NUMERICSERV; //令服务显示为端口号
  getnameinfo(c->ai_addr,c->ai_addrlen,host,1024,serv,1024,flags);
  printf("host:%s,serv:%s\n",host,serv); //打印 ip 及端口号
  
  freeaddrinfo(c);  //事件起点,释放套接字结构 c                 
  if(!c) 
        printf("c is gone\n");//如果 c 被释放了,打印“c is gone”
  else
 	printf("c is there\n"); //如果 c 没有被释放,打印“c is there”
	
}

运行结果: bash-3.2$ ./a.out host:123.58.180.8,serv:80 c is there

为什么这个结构 c 明明被我 freeaddrinfo 了,还是依然阴魂不散呢?

2356 次点击
所在节点    程序员
16 条回复
WordTian
2019-09-25 22:12:49 +08:00
你不是专门写 C 的吧,很明显的野指针啊
ysc3839
2019-09-25 22:42:54 +08:00
因为你只是把 c 的值传给了 freeaddrinfo,freeaddrinfo 没办法修改 c。
LongMaoz
2019-09-25 23:26:55 +08:00
编程没有玄学
si
2019-09-25 23:43:38 +08:00
freeaddrinfo 没有置 0,只是释放,你要主动置 0。
fengtons
2019-09-26 00:04:42 +08:00
c = NULL;
opengps
2019-09-26 00:37:43 +08:00
猪厂是哪?
msg7086
2019-09-26 00:45:51 +08:00
学一下 C 吧。
指针指向 0 不代表内存被释放。
内存被释放不代表指针会指向 0。
两者是独立事件,所以你代码和注释都是错的。
b00tyhunt3r
2019-09-26 00:51:29 +08:00
谢过各位大佬!
happy7902
2019-09-26 01:14:45 +08:00
高手,这是高手。
Mutoo
2019-09-26 07:26:17 +08:00
getaddrinfo 的参数是 &c
而 freeaddrinfo 的参数是 c
前者传的是指针 c 的地址,可以利用此修改该地址的值,即修改 c 本身
后者则是值传递,传的是指针 c 的内容,顾无法修改 c 本身
qakito
2019-09-26 07:51:39 +08:00
函数形参了解一下
agostop
2019-09-26 09:22:13 +08:00
感觉这像是 java 程序员在写 C
LeeSeoung
2019-09-26 09:53:00 +08:00
java 也不是这么写的 楼上别引战。。
catcn
2019-09-26 10:15:12 +08:00
freeaddrinfo 传入的指针类型,是值传递,肯定不会修改或者设置为 NULL 啦。。
dosmlp
2019-09-26 15:36:17 +08:00
楼主去学学 c 语言基础吧
xdtr
2019-09-26 16:54:48 +08:00
getaddrinfo 后 c 指向一个 addrinfo 结构链表。“The getaddrinfo() function allocates and initializes a linked list of addrinfo structures, one for each network address that matches node and service, subject to any restrictions imposed by hints, and returns a pointer to the start of the list in res. The items in the linked list are linked by the ai_next field.”
由于调用 getaddrinfo 后动态分配内存,必须通过调用 freeaddrinfo 释放内存。“The freeaddrinfo() function frees the memory that was allocated for the dynamically allocated linked list res”。
这就很清楚了,freeaddrinfo 并不会释放 c,所以 c 必须手动释放。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/604211

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX