亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1612 | 回復(fù): 4
打印 上一主題 下一主題

指針的一些基礎(chǔ)毛病,建議新手先看看,請經(jīng)驗(yàn)者補(bǔ)充 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2005-12-04 22:47 |只看該作者 |倒序?yàn)g覽
看到大量的內(nèi)容類似的基本指針問題日復(fù)一日的出現(xiàn),總會讓常來逛逛的人覺得心里變得有點(diǎn)“躁”。建議能不能集中一下,先扔塊磚,看這個貼子命運(yùn)如何...

譯自《FreeBSD Developers' Handbook :2.4.1.9》

When my program dumped core, it said something about a “segmentation fault”. What is that?
<<我程序產(chǎn)生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:
<<基本這意味著你的程序嘗試對內(nèi)存執(zhí)行一些非法操作;UNIX設(shè)計(jì)為保護(hù)操作系統(tǒng)及其他程序免受不良程序侵害。

常有的原因?yàn)椋?gt;>

    * Trying to write to a NULL pointer, eg
           <<嘗試寫入一個NULL指針,例如>>

  1. char *foo = NULL;
  2. strcpy(foo, "bang!");
復(fù)制代碼


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

  1. char *foo;
  2. strcpy(foo, "bang!");
復(fù)制代碼


      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.
    <<這個指針會有一個隨機(jī)值,如果運(yùn)氣好的話,它將會指向一個你程序不可用的內(nèi)存區(qū)域,那么內(nèi)核將會在它造成什么傷害前干掉你的程序。如果你運(yùn)氣不好,那么它可能指向某個屬于你程序的地方然后毀掉你的某個數(shù)據(jù)結(jié)構(gòu),導(dǎo)致程序莫名其妙的失敗。>>

    *Trying to access past the end of an array, eg
           <<嘗試超出數(shù)組末端的訪問,例如>>

  1. int bar[20];
  2. bar[27] = 6;
復(fù)制代碼

      
    *Trying to store something in read-only memory, eg
          <<嘗試向只讀(屬性)內(nèi)存內(nèi)放入什么,例如>>

  1. char *foo = "My string";
  2. strcpy(foo, "bang!");
復(fù)制代碼


      UNIX compilers often put string literals like "My string" into read-only areas of memory.
      UNIX的編譯器常將“My sring”這類字符串常量放入內(nèi)存中的只讀(屬性)區(qū)域。

    *Doing naughty things with malloc() and free(), eg
           <<對malloc(),free()做不適當(dāng)操作,例如>>

  1. char bar[80];
  2. free(bar);
復(fù)制代碼

      or <<或>>

  1. char *foo = malloc(27);
  2. free(foo);
  3. free(foo);
復(fù)制代碼


-----     
      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.
      <<犯其中的這些毛病并不一定總是導(dǎo)致錯誤,但這些始終都是壞習(xí)慣。一些系統(tǒng)及編譯器比其他一些更寬容,這就是為什么有些程序曾在一些系統(tǒng)運(yùn)行良好而當(dāng)你在另一些系統(tǒng)嘗試他們的時候會當(dāng)?shù)簟?gt;>
                                                                                              ---------end----------

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2005-12-04 23:45 |只看該作者
不錯!

寫《FreeBSD Developers' Handbook》的人真是細(xì)致入微啊,連這些都要寫,
結(jié)果搞得來后面偶重點(diǎn)關(guān)注的部分老是完不了工。。。

不過也難怪,既然是一本講開發(fā)的手冊,就必然要講到cc;既然講到了cc,就必然
要提及“Common cc Queries and Problems”;你摘錄的這些問題一是屬于
cc的,二也確實(shí)夠common的,所以放到“Common cc Queries and
Problems”一節(jié)中也算是實(shí)至名歸了。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2005-12-04 23:54 |只看該作者
是阿,我要的turnstile就沒有阿...

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2005-12-05 00:09 |只看該作者
------
函數(shù)中的指針返回
1.函數(shù)自己保持有一塊靜態(tài)數(shù)據(jù)結(jié)構(gòu),那么它的指針可以安全返回。不用malloc,free。但缺乏擴(kuò)張性,獲得的指針內(nèi)數(shù)據(jù)僅在下次調(diào)用前有效。例如:

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

  4.         return str;
  5. }
復(fù)制代碼

*返回函數(shù)的非靜態(tài)變量地址是錯誤的。

2.函數(shù)使用malloc()得來的指針。調(diào)用方需要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. }
復(fù)制代碼

*利用棧取得的內(nèi)存指針不能返回,例如alloca(),strdupa()。

3.調(diào)用方提供的指針。任何時候都可以安全返回,但使用它的值前也許要注意檢查。

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


4.函數(shù)內(nèi)調(diào)用其他函數(shù)取得的指針。調(diào)用方是否需要釋放不定。

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

  5. char* bar(const char* str)
  6. {
  7.       return strdup("Here is bar()\n");
  8. }//調(diào)用方需要free()
復(fù)制代碼


------
需要修改指針值的函數(shù)需要接收指針的指針

  1. void foo(char** newptr,size_t len)
  2. {
  3.      *newptr=(char*)malloc(len);
  4. }
復(fù)制代碼

------
注意指針帶入造成的內(nèi)存泄漏

  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. }
復(fù)制代碼

                                                                                        --------暫時想到的,暫停---------

p.s.:為了不成為初學(xué)者互助,歡迎高手們也指導(dǎo)下...

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

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2005-12-05 10:03 |只看該作者
Thanks . 收了!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP