I'm very close of shooting myself in the head right now. Why in hell does free(xx[0]) give me an error: *** glibc detected *** free(): invalid pointer: 0x08ce6158 ***
Please anyone. This is driving me crazy!
Thank you!
Code:
// The function returns something like the argv[] parameter in main. char** parser (char* ch) { char** xx = NULL; char* temp; // contains the input which will be modified when using strtok int length = strlen (ch); // length of the string char* token; int numOfTokens = 0; int i = 0; // copy to use it for counting tokens, since it gets modified (can't use ch) temp = (char*)malloc (length + 1); strcpy (temp, ch); token = strtok(temp, " "); while (token != NULL) { numOfTokens++; token = strtok(NULL, " "); } xx = (char**)malloc (1 * numOfTokens + 1); for (i = 0; i < numOfTokens; i++) xx[i] = (char*)malloc (50); token = strtok(ch, " "); strcpy(xx[0],token); // A living proof that show xx[0]is freeable i = 1; while ((token = strtok(NULL, " \n")) != NULL) { strcpy (xx[i], token); i++; } xx[i] = NULL; // Last arg is NULL free (token); free (xx[0]); // my bane!!!!! free (temp); return xx; }
Thank you!
Comment