c - 重新分配(再次)

标签 c realloc

又是realloc的问题。看来我在之前的很多realloc语句中都没有发现类似的问题。我将不胜感激您的兴趣。

我正在尝试读取格式的文本输入: g:<expression>;0,1,0,1,0 。 我在源文本文件中有一组这种格式的行,以及在“for”循环中读取此行的代码片段(如下)。 : 之间的表达式;被读入propstr二维字符数组。 ;之后的一切确定由逗号分隔的数字组成的 vector (大小为 DIM ) , 。该 vector 被读入 upstr (就像一个字符串)然后转换为整数数组 upvec按功能process_update_vector 。在循环的每次迭代 realloc用于调整上述数组的大小( propstrupstrupvec )。到目前为止在循环中读取的行数是 NREAC 。这是代码:

/*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一切都很好并且总是有效。在某些时候,我不得不增加 DIM最多 11,然后在解析过程中间发生错误(我猜这是常见错误 *** glibc detected *** dinamica: realloc(): invalid pointer: 0x000000000165d190 *** )。 NREAC的值似乎不影响重新分配行为。它始终是代码中发生错误的同一位置。我是否错误地将内存分配给 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;
}

最佳答案

好的,我试着看看你的代码。我的眼睛很酸,但我还是成功地通过了代码。这是我对第一部分的更改,您可以在其中阅读冒号和分号之间的表达式。我更改了一些类型,但让或多或少相同的错误处理,即使我认为这是矫枉过正或者更好地说,它位于错误的位置(我倾向于将分配/错误工作与业务代码分开,使得更容易调试)。

/*Before this point, current line in the source is read into `temp'*/

char **propstr=NULL;         /* I hope this variable was initialized to NULL or else you get problems */
NREAC++;                     /* This is bad naming, all uppercase is by convention reserved for macros */
char *colon = strchr(temp, ':');     /* There a lib function to do the searching, use them */
char *semicolon = strchr(temp, ';');

if(!colon || !semicolon) {
  fprintf(stderr,"Error: syntax error\n");
  return 2112;  /* whatever */
}

ptrchar=realloc(propstr,NREAC*sizeof(char *));     /* realloc called with a NULL pointer is the same as a malloc, typecasts of mallocs/reallocs are not good. */
if(!ptrchar) {
 fprintf(stderr,"Error: could not allocate memory for propstr\n");
 free(propstr);   /* The check against NULL is also done by free, it's therefoe redundant */
 return 1345;
}
else
  propstr=ptrchar;   /* There's no point in NULLing a variable that will be overwritten anyway */

size_t lenexpr = semicolon-colon;   /* The length of the expression can be found by subtracting both pointers */
propstr[NREAC-1]=malloc(lenexpr+1);  /* +1 for the \n */
if(!propstr[NREAC-1]) {
 fprintf(stderr,"Error: couldn't get memory for propstr[NREAC-1]\n");
 return 1344;
}
memcpy(propstr[NREAC-1], colon+1, lenexpr);   /* We copy directly without a temporary that way */
propstr[NREAC-1][lenexpr] = '\n';             /* add the linefeed */
propstr[NREAC-1][lenexpr+1] = 0;              /* terminate the string */

在这里我停了下来,因为你的第二部分有一个我不明白的根本错误。您想将 vector 存储为字符串还是整数数组。如果是前者,那么您必须分配字符而不是 sizeof (int) ,如果是后者,一定有一些atoistrtol某处。

当您向 SO 提交问题时,还有其他一些事情会很好,您应该包括您使用的变量的声明,您应该显示您使用的宏的定义。

编辑:第二部分

// Edit3 ptrchar=realloc(upstr, NREAC*sizeof(char *));

// Edit3 if(!ptrchar) {
// Edit3   fprintf(stderr,"Error could not allocate memory for upstr\n");
// Edit3   free(upstr);
// Edit3   return 1343;
// Edit3 }
// Edit3 else
// Edit3   upstr=ptrchar;

// Edit3 upstr[NREAC-1] = malloc(strlen(semicolon)+1);   /* +1 for the \n */

// Edit3 if(!upstr[NREAC-1]) {
// Edit3  fprintf(stderr,"Error: couldn't get memory for upstr[NREAC-1]\n");
// Edit3  return 1342;
// Edit3 }
if(strlen(semicolon)<2) {/*No vector is specified*/
  fprintf(stderr,"Error: no update vector found:\n'%s'", temp);
  return 1;
}
ptrint = realloc(upvec, NREAC*sizeof(int *));/*!!!!!!!!!!!!!!!!!!!!*/
if(!ptrint) {
  fprintf(stderr,"Error: could not allocate memory for upvec\n");
  free(upvec);
  return 1341;
}
else
  upvec=ptrint;

upvec[NREAC-1] = malloc(DIM*sizeof(int));
if(!upvec[NREAC-1]) {
  fprintf(stderr,"Error: couldn't get memory for upvec[NREAC-1]\n");
  return 1340;
}
// Edit3 memcpy(upstr[NREAC-1], semicolon+1, strlen(semicolon+1)+1);  /* +1 will include the \0 */
// Edit3 strcat(upstr[NREAC-1], "\n"); /*Include newline for more convenient way of parsing*/

/*Get update vector*/
// Edit3 upvec[NREAC-1] = process_update_vector(upstr[NREAC-1], upvec[NREAC-1]);
// Edit3, let's reuse our initial pointer, it's still valid.
process_update_vector(semicolon+1, upvec[NREAC-1]);
continue;

该函数的签名process_update_vector看起来很奇怪,它是否重新分配upvec[NREAC-1] ?如果不是,则返回它并重新分配它是没有意义的。因此,最好也展示该功能。

结论:我在您的代码中检测到的唯一错误是,由于附加的\n,分配的长度 1 太短。 其他点:通过替换if(first) malloc else realloc通过我的realloc您必须确保该指针最初为 NULL,否则就会出现问题。

编辑2:这里是process_update_vector的更新版本,本身并没有错误,但它的作用有点复杂。它还具有很高的缓冲区溢出风险,临时缓冲区只有 5 个字符!

此版本不需要临时缓冲区。

void process_update_vector(const char *upstr, int *upvec)
{
const char *p = strchr(upstr, ',');     /* There are fine library functions for string handling */
int k = 0;
  while(p) {
    upvec[k++] = atoi(upstr);
    upstr = p+1;                        /* Position upstr to after , */

    p = strchr(upstr, ',');
  }
  upvec[k++] = atoi(upstr);
  /* We don't need to return upvec, it doesn't change in the function */
}

两条评论: - 没有对 DIM 进行检查,因此在输入损坏时我们可能会出现缓冲区溢出。 - 没有任何空格处理,通常在逗号后输入字符串(它更具可读性),这种情况在这种情况下不起作用,但有几个 while(*p==' ') p++;放置在正确的位置可以解决这个问题。

编辑3: 被调用函数的变化也会改变调用者,不需要复制到upstr,这样就可以完全删除分配。我添加了// Edit3第二条 list 中的评论。当然,除非您打算在其他地方重复使用复制的字符串。

PS:在此,感谢是通过对答案进行投票来完成的。

关于c - 重新分配(再次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13049079/

相关文章:

c - 将动态数组传递给 C 中的函数

c - 如何使用 fgets() 重新分配多个数组

C 到 Rust 并在 Rust 端返回 "pointer being freed was not allocated"

c - 使用指针从地址读取值 - C 编程

c++ - 奇怪的重新分配行为

c - 使用 malloc() 和 realloc() 将字符串存储在数组中

c重新分配(): invalid next size

c - 管理 Code::Blocks 中的默认头文件目录

c - 关于C语言在GCC、Linux环境下的指针

c - 将动态指针数组传递给 C 函数