2014-01-11 54 views
0

当我运行下面的代码,它围绕变量每个Stucked,我不明白为什么。 该函数会记录一个数据库(一个关闭的txt文件),一个患者和一个指向大小的指针。堆栈周围变量每个/临时

关于功能: 该函数返回一个人数组,其中包含潜在捐助者的所有详细信息,并且与患者至少有'min_match'个相同的基因。

如果捐献者和患者之间没有完全匹配,可以通过部分匹配完成移植。通过将min_match设置为3,该阵列将包含所有具有3个或更多匹配基因的潜在供体作为患者。匹配基因是与患者的相同基因具有完全相同序列的基因。 该数组排序如下;第一标准:按匹配基因的数量。第二个标准:如果两个潜在供体之间匹配基因的数目相同,则优先给予每个基因具有最少错配的供体(在基因的相同位置上DNA序列的差异)。第三个标准:如果两个捐助者具有完全相同的遗传概况,则优先级按字母顺序给出,出现在电话簿的前面。

结构和功能的定义如下:

typedef struct person_{ 
    char name[31]; 
    char id[10]; 
    char genes[5][22]; 
} person; 

person* getPotentialDonors(char* database, person patient, int min_match,int *size){ 
    FILE *db; 
    person * matches,per,temp; 
    int count,i,j=0,count_per=0,index,big=0,k,countS,countJ,MismatchS,MissmatchJ, minMissS, minMissJ,ind; 

    db=fopen(database,"r"); 
    if(!db) exit(1); 

    while(fscanf(db,"%30[^$] %9[^$] %21[^$] %21[^$] %21[^$] %21[^$] %21[^$]%*c",per.name,per.id,per.genes[0],per.genes[1],per.genes[2],per.genes[3],per.genes[4])==7) 
    { 
     count=0; 
     for(i=0;i<5;i++) 
      if(!strcmp(patient.genes[i],per.genes[0])||!strcmp(patient.genes[i],per.genes[1])||!strcmp(patient.genes[i],per.genes[2])||!strcmp(patient.genes[i],per.genes[3])||!strcmp(patient.genes[i],per.genes[4])) count++; 

     if(count>=min_match) count_per++; 
    } 


    matches=(person*)malloc(sizeof(person)*count_per); 

    rewind(db); 

    while(fscanf(db,"%30[^$] %9[^$] %21[^$] %21[^$] %21[^$] %21[^$] %21[^$]%*c",per.name,per.id,per.genes[0],per.genes[1],per.genes[2],per.genes[3],per.genes[4])==7) 
    { 
     count=0; 
     for(i=0;i<5;i++) 
      if(!strcmp(patient.genes[i],per.genes[i])) count++; 

     if(count>=min_match) 
     { 
      strcpy(matches[j].name,per.name); 
      strcpy(matches[j].id,per.id); 
      strcpy(matches[j].genes[0],per.genes[0]); 
      strcpy(matches[j].genes[1],per.genes[1]); 
      strcpy(matches[j].genes[2],per.genes[2]); 
      strcpy(matches[j].genes[3],per.genes[3]); 
      strcpy(matches[j].genes[4],per.genes[4]); 
      j++; 
     } 
    } 

    for(index=0;index<count_per;index++){ 
     big = index; 
     for(j=index+1;j<count_per;j++){ 
      MismatchS = 0; 
      MissmatchJ = 0; 
      minMissJ =minMissS =21; 
      countS=countJ=0; 
      for(k=0;k<5;k++){ 
       MismatchS = 0; 
       if(!strcmp(matches[big].genes[k],patient.genes[k])) countS++; 
       else 
       { 
        for(i=0;i<22;i++){ 
         if(matches[big].genes[k][i]!=patient.genes[k][i]) MismatchS++; 
        } 
        if(MismatchS<minMissS) minMissS=MismatchS; 
       } 
       if(!strcmp(matches[j].genes[k],patient.genes[k])) countJ++; 
       else 
       { 
        for(i=0;i<22;i++){ 
         if(matches[j].genes[k][i]!=patient.genes[k][i]) MissmatchJ++; 
        } 
        if(MissmatchJ<minMissJ) minMissJ=MissmatchJ; 
       } 
      } 
      if(countS < countJ) 
       big = j; 
      else if (countS == countJ && minMissJ<minMissS) big=j; 
      else if (countS == countJ && minMissJ==minMissS && strcmp(matches[j].name,matches[big].name)<0) big=j; 
     } 

     strcpy(temp.name,matches[index].name);//Index->temp 
     strcpy(temp.id,matches[index].id); 
     for(ind=0;ind<5;ind++) 
      strcpy(temp.genes[ind],matches[index].genes[ind]); 


     strcpy(matches[index].name,matches[big].name);//big->index 
     strcpy(matches[index].id,matches[big].id); 
     for(ind=0;ind<5;ind++) 
      strcpy(matches[index].genes[ind],matches[big].genes[ind]); 


     strcpy(matches[big].name,temp.name);//temp->big 
     strcpy(matches[big].id,temp.id); 
     for(ind=0;ind<5;ind++) 
      strcpy(matches[big].genes[ind],temp.genes[ind]); 
    } 

    fclose(db); 
    *size = count_per; 

    return matches; 
} 
+0

困惑“卡住”是什么意思? –

+0

当我尝试运行代码时,在VS2010中,当min_match = 1时,我收到messenge运行时检查失败#2 - 变量“per”周围的堆栈已损坏。 – user2600366

+0

这是一个很好的运行时检查,它告诉你,你的代码溢出了一个堆栈缓冲区。对于那些硬编码字符串长度的潜力很大,并且根本没有检查文件中的数据实际上是否适合字符串缓冲区。 –

回答

0

也许没有答案的问题,但我的对称检测器由

for(i=0;i<5;i++) 
if(!strcmp(patient.genes[i],per.genes[0]) ||!strcmp(patient.genes[i],per.genes[1])||!strcmp(patient.genes[i],per.genes[2]) ||!strcmp(patient.genes[i],per.genes[3])||!strcmp(patient.genes[i],per.genes[4])) count++; 
//do something with count 

for(i=0;i<5;i++) 
    if(!strcmp(patient.genes[i],per.genes[i])) count++; 
// do something with count that is supposed to be the same as above 
+0

第一个for循环会更新计数器,以便计算数组大小。第二个是为了检查每行是否与数组匹配。 – user2600366

相关问题