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

时间:2014-12-10 15:33:31

标签: c

这是我从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;
}

这就是我在main中使用它的方式:

    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);
}

1 个答案:

答案 0 :(得分:0)

你通过递增它会丢失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
}