免费注册 查看新帖 |

Chinaunix

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

指针的一些基础毛病,建议新手先看看,请经验者补充 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-04 22:47 |只看该作者 |倒序浏览
看到大量的内容类似的基本指针问题日复一日的出现,总会让常来逛逛的人觉得心里变得有点“躁”。建议能不能集中一下,先扔块砖,看这个贴子命运如何...

译自《FreeBSD Developers' Handbook :2.4.1.9》

When my program dumped core, it said something about a “segmentation fault”. What is that?
<<我程序产生core dump的时候,它说类似“segmentation fault”之类的话,这是什么?>>

This basically means that your program tried to perform some sort of illegal operation on memory; UNIX is designed to protect the operating system and other programs from rogue programs.

Common causes for this are:
<<基本这意味着你的程序尝试对内存执行一些非法操作;UNIX设计为保护操作系统及其他程序免受不良程序侵害。

常有的原因为:>>

    * Trying to write to a NULL pointer, eg
           <<尝试写入一个NULL指针,例如>>

  1. char *foo = NULL;
  2. strcpy(foo, "bang!");
复制代码


    *Using a pointer that has not been initialized, eg
           <<使用未初始化指针,例如>>

  1. char *foo;
  2. strcpy(foo, "bang!");
复制代码


      The pointer will have some random value that, with luck, will point into an area of memory that is not available to your program and the kernel will kill your program before it can do any damage. If you are unlucky, it will point somewhere inside your own program and corrupt one of your data structures, causing the program to fail mysteriously.
    <<这个指针会有一个随机值,如果运气好的话,它将会指向一个你程序不可用的内存区域,那么内核将会在它造成什么伤害前干掉你的程序。如果你运气不好,那么它可能指向某个属于你程序的地方然后毁掉你的某个数据结构,导致程序莫名其妙的失败。>>

    *Trying to access past the end of an array, eg
           <<尝试超出数组末端的访问,例如>>

  1. int bar[20];
  2. bar[27] = 6;
复制代码

      
    *Trying to store something in read-only memory, eg
          <<尝试向只读(属性)内存内放入什么,例如>>

  1. char *foo = "My string";
  2. strcpy(foo, "bang!");
复制代码


      UNIX compilers often put string literals like "My string" into read-only areas of memory.
      UNIX的编译器常将“My sring”这类字符串常量放入内存中的只读(属性)区域。

    *Doing naughty things with malloc() and free(), eg
           <<对malloc(),free()做不适当操作,例如>>

  1. char bar[80];
  2. free(bar);
复制代码

      or <<或>>

  1. char *foo = malloc(27);
  2. free(foo);
  3. free(foo);
复制代码


-----     
      Making one of these mistakes will not always lead to an error, but they are always bad practice. Some systems and compilers are more tolerant than others, which is why programs that ran well on one system can crash when you try them on an another.
      <<犯其中的这些毛病并不一定总是导致错误,但这些始终都是坏习惯。一些系统及编译器比其他一些更宽容,这就是为什么有些程序曾在一些系统运行良好而当你在另一些系统尝试他们的时候会当掉。>>
                                                                                              ---------end----------

论坛徽章:
0
2 [报告]
发表于 2005-12-04 23:45 |只看该作者
不错!

写《FreeBSD Developers' Handbook》的人真是细致入微啊,连这些都要写,
结果搞得来后面偶重点关注的部分老是完不了工。。。

不过也难怪,既然是一本讲开发的手册,就必然要讲到cc;既然讲到了cc,就必然
要提及“Common cc Queries and Problems”;你摘录的这些问题一是属于
cc的,二也确实够common的,所以放到“Common cc Queries and
Problems”一节中也算是实至名归了。

论坛徽章:
0
3 [报告]
发表于 2005-12-04 23:54 |只看该作者
是阿,我要的turnstile就没有阿...

论坛徽章:
0
4 [报告]
发表于 2005-12-05 00:09 |只看该作者
------
函数中的指针返回
1.函数自己保持有一块静态数据结构,那么它的指针可以安全返回。不用malloc,free。但缺乏扩张性,获得的指针内数据仅在下次调用前有效。例如:

  1. char* foo(void)
  2. {
  3.         static char str[256];

  4.         return str;
  5. }
复制代码

*返回函数的非静态变量地址是错误的。

2.函数使用malloc()得来的指针。调用方需要free()

  1. char* foo(size_t len)
  2. {
  3.         char* ptr;
  4.         ptr=(char*)malloc(len);
  5.         return ptr;
  6. }

  7. void bar(void)
  8. {
  9.          char * ptr;
  10.          ptr=foo(128);
  11.          free(ptr);
  12. }
复制代码

*利用栈取得的内存指针不能返回,例如alloca(),strdupa()。

3.调用方提供的指针。任何时候都可以安全返回,但使用它的值前也许要注意检查。

  1. char* foo(char* arg)
  2. {
  3.          char ch='\0';
  4.          
  5.          if(arg!=NULL)  ch=*arg;
  6.         
  7.          return arg;
  8. }
复制代码


4.函数内调用其他函数取得的指针。调用方是否需要释放不定。

  1. char* foo(const char* base,const char* sub)
  2. {
  3.       return strstr(base,sub);
  4. }//调用方不需要free()

  5. char* bar(const char* str)
  6. {
  7.       return strdup("Here is bar()\n");
  8. }//调用方需要free()
复制代码


------
需要修改指针值的函数需要接收指针的指针

  1. void foo(char** newptr,size_t len)
  2. {
  3.      *newptr=(char*)malloc(len);
  4. }
复制代码

------
注意指针带入造成的内存泄漏

  1. char* foo(void)
  2. {
  3.      return malloc(100);
  4. }

  5. void bar(void)
  6. {
  7.         char* ptr;
  8.         ptr=foo();
  9.         printf("%x\n",ptr);
  10.         ptr=foo(); //causing memory leak
  11. }
复制代码

                                                                                        --------暂时想到的,暂停---------

p.s.:为了不成为初学者互助,欢迎高手们也指导下...

[ 本帖最后由 zalem 于 2005-12-5 00:10 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2005-12-05 10:03 |只看该作者
Thanks . 收了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP