c - Can I/ Should I free pointer returned by strtok -


so dynamically creating array of strings. assigning each element in array pointer returned calling strtok. @ end of process when need redo have been trying free pointers in elements of said array, keep getting error stating

*** glibc detected *** ./prgm: munmap_chunk(): invalid pointer: 0x00007fff600d98

also, make sense free inputstr @ end of loop?

where logical "not logical @ all" thinking wrong.. e.g code

   char** argvnew = (char**)calloc(33,sizeof(char*));    char inputstr[128];      do{             scanf("%127[^\n]%*c", inputstr);         token = strtok(inputstr, delim);          /* add tokens array*/         varnum= 0;         for(i = 0; token != null; i++){             varnum++;             argvnew[i] = token;             token = strtok(null, delim);         }         argvnew[i] = null;         //free argvnew          for(i = 0; < varnum;i++){             printf("deleting %i, %s\n",i,argvnew[i]);             free(argvnew[i]);         }   while(1); 

no, should not free it. it's returning pointer character in inputstr (or null when reaches end). it's not allocating new memory, there's nothing free.

if inputstr dynamically allocated, should free when you're done it.


Comments