-9

又是 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;
}
4

2 回答 2

2

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

/*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 */

在这里我停了下来,因为你的第二部分有一个我不明白的基本错误。您要将向量存储为字符串还是整数数组。如果是前者,那么你必须分配 chars 而不是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似乎很奇怪,它 reallocupvec[NREAC-1]吗?如果没有,则返回并重新分配它是没有意义的。因此,展示该功能也是一个好主意。

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

EDIT2:这里是 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++;放置在正确位置的几个可以解决这个问题。

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

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

于 2012-10-25T08:33:08.343 回答
0

我使用 Microsoft Visual Studio 2005 将代码粘贴到一个临时文件中,并使用重新格式化命令将其重新格式化为下面的文本。

请参阅这篇关于编程风格的 Wikipedia 文章,它可以帮助您避免因 stackoverflow 提交而受到抨击。

另请参阅此C 编码样式文档示例

编辑开始 运行时抱怨一个无效的指针,这表明您传递给realloc()的指针不是通过调用创建的指针malloc()calloc()。它知道,因为每当您执行 malloc() 时,都会有一个内存管理标头,它是数据区域的一部分,并且给您的指针是指向标头之后分配的内存的指针。看到这个问题realloc 如何知道要复制多少?

最后,我使用状态机方法重写了它(另请参阅状态机说明)并将该源代码放在重新格式化示例的底部。 编辑结束

查看源代码,这似乎是循环的一部分。看来变量 upvec 可能没有被初始化,它是某种数组,所以它实际上是否被分配了?

请参阅带//--------------------指示的部分。

重新格式化的来源如下:

    /*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;

编辑 使用具有解析每一行文本的函数的状态机来解决此问题的建议方法。

#include <malloc.h>
#include <stdlib.h>

// pLine is a line of text containing a zero terminated string of the format of
//    g:<expression>;0,1,0,1,0
// This function will process the line and return the expression as a string
// and a list of the integers.
void processExpression (char *pLine, char *pExpression, int **pIntList)
{
    int  stateMachineIndex = 1;
    int  integerCount = 0;
    char *pLineSave = 0;
    int  iIntListIndex = 0;

    *pIntList = 0;

    while (*pLine) {
        switch (stateMachineIndex) {
            case 1:
                // initial state
                if (*pLine == ':') {
                    // colon found so now start getting the expression
                    stateMachineIndex = 2;
                }
                pLine++;
                break;
            case 2:
                if (*pLine != ';') {
                    *pExpression++ = *pLine++;
                } else if (*pLine) {
                    // if we have not reached end of string yet then go to the
                    // next state of parsing the list of integers.
                    stateMachineIndex = 3;
                    pLine++;
                    pLineSave = pLine;
                }
                break;
            case 3:
                // at this point we begin to process the list of integers.
                // however we are not sure how many there are so we will count them first
                if (*pLine == ',') {
                    integerCount++;
                }
                pLine++;
                break;
            case 4:
                // we now have a count of the integers we expect however it
                // may be off by one so we will allocate a smidge more space
                *pIntList = (int *)calloc ((integerCount + 2), sizeof(int));
                stateMachineIndex = 5;
                *pExpression = 0;         // and while we are at it lets terminate our expression string
                break;
            case 5:
                // now we get an integer value from the list of integers
                (*pIntList)[iIntListIndex++] = atoi (pLine);
                // eat up characters to the next integer in the list
                while (*pLine && *pLine != ',') pLine++;
                if (*pLine == ',') pLine++; // if we found a comma, skip it to the next field
                break;
            default:
                break;
        }
        if (*pLine == 0 && stateMachineIndex < 4) {
            // end of the string so now lets do our integer thing
            // if we are still in the first phase of processing
            if (pLineSave && *pLineSave && integerCount > 0) {
                stateMachineIndex = 4;
                pLine = pLineSave;    // restart our parser back to the integer area
            } else {
                break;
            }
        }
    }
}

// simple test harness to test the concept.
int main(int argc, char* argv[])
{
    char *pLine = "g:expression and stuff;1,2,3,4,5";
    char expressionBuffer[128];
    int  *pIntList = 0;

    processExpression (pLine, expressionBuffer, &pIntList);
    return 0;
}
于 2012-10-24T12:53:41.880 回答