Getting invalid values ​​when accessing variables passed in a pointer to a character array for strtok

Here is my code

//Split up the config by lines int x; int numberOfConfigLines = 0; for (x = 0; x < strlen(buffer); x++) { if (buffer[x] == '\n') { numberOfConfigLines++; } } char *configLines[numberOfConfigLines]; tokenize(configLines, buffer, "\n", numberOfConfigLines); 

The idea behind this function is to count the number of new lines in the buffer, and then split the buffer into the strtok array using this:

 #include <string.h> #include <stdlib.h> void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) { //Create a clone of the original string to prevent making permanent changes to it char *tempString = (char *)malloc(strlen(delimitedString) + 1); strcpy(tempString, delimitedString); if (expectedTokenArraySize >= 1) { arrToStoreTokens[0] = strtok(tempString, delimiter); int x; for (x = 1; x < expectedTokenArraySize; x++ ) { arrToStoreTokens[x] = strtok(NULL, delimiter); } } //Dispose of temporary clone free(tempString); } 

If I directly access arrToStoreTokens[0] , I get the correct result, however, when I try to access configLines[0] after the tokenize function tokenize , I get different results (may be unknown or just empty)

In addition, I believe that this only started after I started running the program as root (for another requirement). Maybe I'm wrong. - EDIT: confirmed that this is not a problem.

Any ideas?

+5
source share
1 answer

strtok does not redistribute anything. It only makes clippings and pointers of what you gave him.

Your array stores pointers that strtok provides but do not copy the contents.

So, if you free your tempString variable, you will get free data that was indicated by strtok return values. You must save it and release it only at the end.

Or you can make strdup each strtok return to store it in your array to make a real copy of each token, but in this case you will need to free each token at the end.

The second solution will look like this:

 void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) { //Create a clone of the original string to prevent making permanent changes to it char *tempString = (char *)malloc(strlen(delimitedString) + 1); strcpy(tempString, delimitedString); if (expectedTokenArraySize >= 1) { arrToStoreTokens[0] = strdup(strtok(tempString, delimiter)); // Here is the new part : strdup int x; for (x = 1; x < expectedTokenArraySize; x++ ) { arrToStoreTokens[x] = strdup(strtok(NULL, delimiter)); // Here is the new part : strdup } } //Dispose of temporary clone free(tempString); } 

And after using this array, you will have to delete it using the following function:

 void deleteTokens(char **arrToStoreTokens, int arraySize) { int x; for (x = 0; x < arraySize; ++x) { free(arrToStoreTokens[x]); } } 
+4
source

All Articles