繁体   English   中英

如何释放从main调用函数中分配的char **数组?

[英]How to free char** array that allocated in calling function from main?

这是我从main调用的功能:

char** readTokens(char *userInput, const char *seperator)
{
    char** c;
    char line1[512],line2[512];
    int wordCount = 0;
    int index;
    char* tmp;
    strcpy(line1, userInput);

    for (index=0;line1[index]!='\n';index++);

    line1[index]='\0';
    strcpy(line2,line1);
    tmp = strtok(line1,seperator);
    while (tmp!=NULL)
    {
        tmp=strtok(NULL,seperator);
            wordCount = wordCount + 1;
    }
    if((wordCount) == ERROR)
    {
            return NULL;
    }

  c=(char**)malloc(((wordCount)+1)*sizeof(char*));
  if (c == NULL) 
  {
    printf("failed to allocate memory.\n");
    return NULL;
  }

  tmp = strtok(line2,seperator);
  index=0;

  while (tmp!=NULL)
  {
    c[index]=(char*)malloc((strlen(tmp)+1*sizeof(char)));  
    if (c[index]==NULL)
    {
            printf("failed to allocate memory.\n");
            return NULL;   
        }

    strcpy(c[index],tmp);
    tmp=strtok(NULL,seperator);
    index++;
  }

  c[index] = NULL;//put NULL on last place  

  return c;
}

这就是我主要使用它的方式:

    while (fgets(words, sizeof(words), filePointer) != NULL) // this line is a command of reading a line from the file.
    {
        /*here i am calling the function*/
        array = readTokens(words, " ");
        theGraph->graphEdges[index_i].sourceVertex = array[ZERO];
        theGraph->graphEdges[index_i].destinationVertex = array[ONE];
        theGraph->graphEdges[index_i].arcValue = atoi(array[TWO]);

        for(index_j = ZERO ; index_j < vertexes ; index_j++)
        {
            if(theGraph->placeInTableIndex[index_j] == NULL)
            {
                theGraph->placeInTableIndex[index_j] = array[ZERO];
                break;  
            }
            else if(strcmp(theGraph->placeInTableIndex[index_j],array[ZERO]) == ZERO)
                break;
        }

        for(index_j = ZERO ; index_j < vertexes ; index_j++)
        {
            if(theGraph->placeInTableIndex[index_j] == NULL)
            {
                theGraph->placeInTableIndex[index_j] = array[ONE];
                break;  
            }
            else if(strcmp(theGraph->placeInTableIndex[index_j],array[ONE]) == ZERO)
                break;
        }
        theGraph->graphEdges[index_i+ONE].sourceVertex = array[ONE];
        theGraph->graphEdges[index_i+ONE].destinationVertex = array[ZERO];
        theGraph->graphEdges[index_i+ONE].arcValue = atoi(array[TWO]);
        index_i+= TWO;
        //freeTokens(array);

  }

我试图在一段时间结束时对数组进行释放,但是它不起作用,我仍然从该函数中泄漏内存(valgrind检查)。 我正在使用此功能来释放:

void freeTokens(char** tokens)
{
  while(*tokens != NULL)
  {
    *tokens = NULL;
    free(*tokens);
    *tokens++;
  }
  tokens = NULL;
  free(tokens);
}

通过增加tokens您将失去tokens的原始价值(需要释放的东西); 然后将其设置为NULL ,然后尝试释放NULL

代替:

void freeTokens(char** tokens)
{
   char **freeTokens = tokens;

   while (*freeTokens != NULL)
   {
     free(*freeTokens);
     *freeTokens = NULL;   // not actually necessary, but must happen *after* if at all
     freeTokens++;        
   }

   free(tokens);
   // tokens = NULL;    // accomplishes nothing; doesn't change the caller's version
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM