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

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

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4896 | 回復(fù): 2
打印 上一主題 下一主題

建立鏈表的一個(gè)段錯(cuò)誤 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-07-22 10:53 |只看該作者 |倒序?yàn)g覽
本帖最后由 sumland 于 2011-07-22 10:54 編輯
  1. //有一個(gè)8M左右的文件,每一行都是(str1:str2:str3:str4)這樣的格式。本程序讀取文件的每一行,建立鏈表,以不同的str2所在的行分別作為鏈表的不同節(jié)點(diǎn),
  2. //并將具有相同的str2的行合并成一個(gè)節(jié)點(diǎn)的data.
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. typedef struct node
  7. {
  8. char *data;
  9. int num;//記錄是str2字段出現(xiàn)的次數(shù);
  10. struct node *next;       
  11. }listnode,*linklist;


  12. //添加一個(gè)節(jié)點(diǎn),比較之前的節(jié)點(diǎn)的str2,若已有相同值,則串到該節(jié)點(diǎn)后;若無(wú),則新增一個(gè)節(jié)點(diǎn)存儲(chǔ)
  13. listnode *add_line(char *line,listnode *head)
  14. {
  15.         char newbuf[128];
  16.         char oldbuf[128];
  17.         memset(newbuf,0x0,128);
  18.         memset(oldbuf,0x0,128);
  19.         listnode *p=head;
  20.         listnode *q=head;
  21.         int flag=0;
  22.         while(p!=NULL)
  23.         {
  24.          sscanf(line,"%*[^:]:%[^:]",newbuf);
  25.        
  26.          printf("p->data=%s\n",p->data);
  27.          sscanf(p->data,"%*[^:]:%[^:]",oldbuf);
  28.          
  29.          if(strcmp(newbuf,oldbuf)==0)
  30.                  {
  31.                 
  32.                          char *tp=(char *)realloc(p->data,(strlen(p->data)+strlen(line)+1));
  33.                          if(tp==NULL)
  34.                                          {
  35.                                                  printf("realloc fault!\n");
  36.                                                  break;
  37.                                          }
  38.                          else
  39.                                  p->data=tp;
  40.                          strcat(p->data,line);                        
  41.                          p->num+=1;
  42.                          flag=1;
  43.                          break;
  44.                  }
  45.          else                        
  46.                  p=p->next;         
  47.         }
  48.        
  49.         if(flag==0)
  50.         {         
  51.                 while(q->next!=NULL)
  52.                         q=q->next;                                               
  53.                 q->next=(listnode *)malloc(sizeof(listnode));
  54.                 q->next->data=strdup(line);
  55.                 q->next->num=1;                       
  56.         }       

  57. return head;                                       
  58. }



  59. int main(int argc,char *argv[])
  60. {
  61.        
  62. FILE *fp=fopen(argv[1],"r+");
  63. char *line=(char *)malloc(1024);
  64. memset(line,0x0,1024);
  65. size_t len = 0;
  66. ssize_t  read;
  67. //char buf[128];
  68. //getline(&line, &len, fp);

  69. fgets(line,1024,fp);
  70. listnode *head =(listnode *)malloc(sizeof(listnode));
  71. head->data=strdup(line);
  72. head->num=1;
  73. head->next=NULL;
  74. memset(line,0x0,1024);       
  75. //        while ((read = getline(&line, &len, fp)) != -1)
  76. while(fgets(line,1024,fp)!=NULL)
  77. {                                                                                 
  78.         add_line(line,head);
  79.         memset(line,0x0,1024);                                                                        
  80. }

  81. if(line)
  82.     free(line);
  83.    
  84. listnode *nw=head;  
  85. int i=0;
  86. while(nw!=NULL)
  87. {
  88. printf("%s,%d,%d\n",nw->data,nw->num,i);
  89. i++;
  90. nw=nw->next;       
  91. }   
  92.        
  93. return 0;       
  94. }
復(fù)制代碼
運(yùn)行出現(xiàn)段錯(cuò)誤,gdb查出
#0  0x08048688 in add_line (line=0x87ac170 "tel:13391181435:ָ6:gb2312\r\n", head=0x87ac57 at myfsort.c:28

warning: Source file is more recent than executable.

28               sscanf(p->data,"%*[^:]:%[^:]",oldbuf);
(gdb) p p->data
Cannot access memory at address 0xcfc93a30

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2011-07-22 13:32 |只看該作者
回復(fù) 1# sumland


    根據(jù)現(xiàn)象分析了一下代碼,發(fā)現(xiàn)可能存在問(wèn)題的語(yǔ)句段如下:
if(flag==0)
        {         
                while(q->next!=NULL)
                        q=q->next;                                                
                q->next=(listnode *)malloc(sizeof(listnode));   //這里malloc之后,沒(méi)有對(duì)數(shù)據(jù)進(jìn)行初始化,特別是malloc出來(lái)的listnode節(jié)點(diǎn)的next域.
                q->next->data=strdup(line);
                q->next->num=1;                        
        }      

由于上述的listnode結(jié)點(diǎn)的next域沒(méi)有初始化,所以很有可能出現(xiàn)不可預(yù)料的值 ,也就會(huì)導(dǎo)致你程序運(yùn)行時(shí),不定期的出現(xiàn)p->data的訪問(wèn)的segment fault

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2011-07-28 11:57 |只看該作者
回復(fù) 2# foolishx


    嗯,謝謝了!
您需要登錄后才可以回帖 登錄 | 注冊(cè)

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP