又是 realloc 问题。在之前的许多 realloc 语句中,我似乎找不到任何类似的问题。我会很感激你的兴趣。
我正在尝试读取格式为: 的文本输入
g:<expression>;0,1,0,1,0。我在源文本文件中有一组这种格式的行,还有一段代码(如下)在“for”循环中读取这一行。和之间的表达式被读入二维字符数组。之后的所有内容都确定了一个由逗号分隔的数字向量(大小) 。这个向量被读入(就像一个字符串),然后通过函数转换成整数数组。在循环的每次迭代中,都用于调整上述数组(和)的大小。到目前为止在循环中读取的行数是。这是代码::;propstr;DIM,upstrupvecprocess_update_vectorreallocpropstrupstrupvecNREAC
/*Before this point, current line in the source is read into `temp'*/
NREAC++;
for(i=0;i<strlen(temp);i++){
 if(temp[i]==':') colon=i;//Here we find colon
 if(temp[i]==';') semicolon=i;//...and semicolon positions
}
memset(temp1,'\0',STRLEN);
if(NREAC==1)
 ptrchar=(char **)malloc(sizeof(char *));
else
 ptrchar=realloc(propstr,NREAC*sizeof(char *));
if(ptrchar==NULL){
 fprintf(stderr,"Error: could not allocate memory for propstr\n");
 if(propstr!=NULL) free(propstr);
 return 1345;
}else{propstr=ptrchar;ptrchar=NULL;}
propstr[NREAC-1]=(char *)malloc((semicolon-colon)*sizeof(char));
if(propstr[NREAC-1]==NULL){
 fprintf(stderr,"Error: couldn't get memory for propstr[NREAC-1]\n");
 return 1344;
}
for(i=colon+1;i<semicolon;i++)/*Copy the propensity part of the line*/
 temp1[i-colon-1]=temp[i];
temp1[i-colon-1]='\n';/*Include newline symbol for correct parsing*/
strcpy(propstr[NREAC-1],temp1);
memset(temp1,'\0',STRLEN);
if(NREAC==1)
 ptrchar=(char **)malloc(sizeof(char *));
else
 ptrchar=realloc(upstr,NREAC*sizeof(char *));
if(ptrchar==NULL){
 fprintf(stderr,"Error could not allocate memory for upstr\n");
 if(upstr!=NULL) free(upstr);
 return 1343;
}else{upstr=ptrchar;ptrchar=NULL;}
upstr[NREAC-1]=(char *)malloc((strlen(temp)-semicolon-1)*sizeof(char));
if(upstr[NREAC-1]==NULL){
 fprintf(stderr,"Error: couldn't get memory for upstr[NREAC-1]\n");
 return 1342;
}
if(strlen(temp)-semicolon==2){/*No vector is specified*/
 fprintf(stderr,"Error: no update vector found:\n");
 fprintf(stderr,"`%s'",temp);
 return 1;
}
if(NREAC==1)
 ptrint=(int **)malloc(sizeof(int *));
else
 ptrint=(int **)realloc(upvec,NREAC*(sizeof(int *)));/*!!!!!!!!!!!!!!!!!!!!*/
if(ptrint==NULL){
 fprintf(stderr,"Error: could not allocate memory for upvec\n");
 if(upvec!=NULL) free(upvec);
 return 1341;
}else{upvec=ptrint;ptrint=NULL;}
upvec[NREAC-1]=(int *)malloc(DIM*sizeof(int));
if(upvec[NREAC-1]==NULL){
 fprintf(stderr,"Error: couldn't get memory for upvec[NREAC-1]\n");
 return 1340;
}
for(i=semicolon+1;i<strlen(temp)-1;i++)
 temp1[i-semicolon-1]=temp[i];
temp1[i-semicolon-1]='\n';/*Include newline for more convenient way of parsing*/
strcpy(upstr[NREAC-1],temp1);
/*Get update vector*/
upvec[NREAC-1]=process_update_vector(upstr[NREAC-1],upvec[NREAC-1]);
memset(temp1,'\0',STRLEN);
memset(temp,'\0',STRLEN);
continue;
此代码段出现在 for 循环中。“无效指针”错误出现在标有 的地方/*!!!!!!!!!!!!!!!!!!!!*/。
错误的条件。对于足够小的DIM一切工作正常并且总是有效。在某些时候,我不得不增加到DIM11,然后错误发生在解析过程的中间(我猜这是通常的错误,*** glibc detected *** dinamica: realloc(): invalid pointer: 0x000000000165d190 ***)。的值NREAC似乎不会影响 realloc 行为。它总是在代码中发生错误的相同位置。我是否错误地为int类型变量分配内存,因为类型分配char从来都不是问题?
process_update_vector功能:
int * process_update_vector(const char *upstr,int *upvec)
{
   int i,j,k;
   char symbuf[5];/*5 symbols, max 99999 is possible*/
   i = 0;
   j = 0;
   k = 0;
   while(upstr[i] != '\n'){
      if(upstr[i] == ','){/*',' is the delimiter*/
         symbuf[j] = '\0';
         j = 0;
         upvec[k] = atoi(symbuf);
         k++;
         i++;
         continue;
      }
      symbuf[j] = upstr[i];
      j++;
      i++;
   }
   /*For the last entry*/
   upvec[k] = atoi(symbuf);
   k++;
   return upvec;
}