免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2867 | 回复: 7
打印 上一主题 下一主题

什么是野指针? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-22 10:15 |只看该作者 |倒序浏览
什么是野指针?

malloc地址使用后free后.那么指向malloc分配的地址指针是不是野指针。

free()后的地址,可以重新使用,但系统可以立即自动回收吗?


请指教。谢!!!

论坛徽章:
0
2 [报告]
发表于 2009-11-22 11:03 |只看该作者
指向未知区域的指针
是的

论坛徽章:
0
3 [报告]
发表于 2009-11-22 19:45 |只看该作者
详细一点。谢!!!

招聘 : Java研发
论坛徽章:
0
4 [报告]
发表于 2009-11-22 20:15 |只看该作者

回复 #1 zouhu_cn 的帖子

你说的没错 是野指针了
例如
p=malloc();
free(p);
最后:p=NULL;
这样可以防止p的在释放的滥用了

论坛徽章:
0
5 [报告]
发表于 2009-11-22 20:35 |只看该作者
应该这样理解:“野”是指不受控制,“野指针”是不受控制的指针

如:int *p;  *p = 0;         // p 指向哪里?不受控制。

而:free(p);         

     free 掉之后的指针,不应理解为“野指针”,而理解为“无效指针”更为合适

论坛徽章:
0
6 [报告]
发表于 2009-11-22 21:58 |只看该作者

回复 #4 lvrainbow 的帖子

不要把p置为NULL, 除非你要把p==NULL当成一个标识

论坛徽章:
0
7 [报告]
发表于 2009-11-23 09:36 |只看该作者
原帖由 lvrainbow 于 2009-11-22 20:15 发表
你说的没错 是野指针了
例如
p=malloc();
free(p);
最后:p=NULL;
这样可以防止p的在释放的滥用了


p==NULL的作用我认为是在你对指针的操作都有if(p !=NULL){};的时候才有作用吧!

不可控的指针就是野指针。。。也就是指针指到哪里你是不知道的。。。
不知道我这样的理解对不对!

可以请高手给我们列举下经常容易出现野指针的情况,让大家多注意!

或者是请高手说下写代码时哪些风格或者写法可以最有效的避免出现野指针!

[ 本帖最后由 bladmin 于 2009-11-23 09:38 编辑 ]

论坛徽章:
8
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:58技术图书徽章
日期:2013-12-04 10:48:50酉鸡
日期:2014-01-03 10:32:30辰龙
日期:2014-03-06 15:04:07
8 [报告]
发表于 2009-11-23 11:45 |只看该作者
Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type.

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, a silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows). If the overwritten data is bookkeeping data used by the system's memory allocator, the corruption can cause system instabilities.

Wild pointers arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same erratic behaviour as dangling pointers, though they are less likely to stay undetected.

http://en.wikipedia.org/wiki/Dangling_pointer


如上,free后继续使用的指针叫做悬挂指针;在设置到一个正确状态前就被引用的指针就是野指针。


避免以上情况的有效办法是设置状态标志,比如用NULL或0xFFFFFFFF之类东西指示失效状态;
任何指针,只要不再有效,就要在第一时间设置将其值改为失效标志;
如果存在并发,那么一定要将“指针失效-设置标志”这两个操作加锁并封装到一个原子性的接口里,并且通过编程规范或其他手段(如句柄)阻断对原始指针的直接访问,以避免外部访问到脏数据。

如果失效标志合理的话,其实是不用在每次引用前都检查。系统自然会在引用错误指针时core掉——因为这种bug必然和程序逻辑相关,所以只要设计合理,这种错误可以轻易发现并杜绝掉。


另外要注意编译器警告,起码要把“使用未初始化的数据”当作错误看(实际上是应该把所有警告当作错误的,不管这警告有多么微不足道)。

[ 本帖最后由 shan_ghost 于 2009-11-23 11:49 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP