1

I have a tokenizer method that returns a char**. The results are being stored in a char** called lineTokens. When I use printf() to print the first token, I get the correct result, but when I use strcmp(lineTokens[0],"Some text"), I get a seg fault. The appropriate code is below.

lineTokens = tokenize(tempString);
printf("token[0] = %s\n", lineTokens[0]);
if(strcmp(lineTokens[0], "INPUTVAR")==0){
    printf("It worked\n");
}

EDIT: My tokenize code is as follows

char** tokenize(char* input){
int i = 0;
char* tok;
char** ret;
tok = strtok(input, " ");
ret[0] = tok;
while(tok != NULL){
    printf("%s\n", tok);
    ret[i] = tok;
    tok = strtok(NULL, " ");
    i++;
}
ret[i] = NULL;
return ret;

}

  • 4
    `I get the correct result,`...a possible side-effect of an [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). – Sourav Ghosh Apr 23 '15 at 13:53
  • 1
    Welcome to StackOverflow! Have you tried using a debugger such as gdb or a memory tool such as valgrind? – Eregrith Apr 23 '15 at 13:53
  • The bug is most likely outside the code posted. – Lundin Apr 23 '15 at 13:54
  • 1
    More details are needed. Post your `tokenize()` code. – Andrew Henle Apr 23 '15 at 13:55
  • Show us your function and the main where you call the function! – Sir Jo Black Apr 23 '15 at 14:00
  • 2
    I bet `tokenize` has a local variable like `char* result[10];` and [it does a `return result;`](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). This returns the address of a local variable. The memory for the local variable is still valid at the `printf`, but `printf` consumes enough stack that by the time it returns, the memory is no longer valid. – Raymond Chen Apr 23 '15 at 14:00
  • `char** ret; ... ret[0] = tok;` is bad. `ret` is not initialized or assigned. – chux - Reinstate Monica Apr 23 '15 at 15:01

2 Answers2

3

It's impossible to answer this without seeing the code of tokenize(), of course.

My guess is that there is some undefined behavior in that function, which perhaps corrupts the stack, so that when printf() runs and actually uses some more stack space, things go bad. The thing with undefined behavior is that it's really undefined, anything can happen.

Run the code in Valgrind.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    Or debug the executable using the Gdb debugger. http://web.eecs.umich.edu/~sugih/pointers/summary.html Small gdb tutorial to start with. – Rndp13 Apr 23 '15 at 13:58
1

Your tokenize function is broken. Every pointer in your code needs to point at allocated memory, which is not the case here. You get no memory allocated by simply declaring a pointer: a pointer is merely containing an address to memory allocated some place else. Given that you set it to point at "some place else", if you don't, it will point at a random garbage address.

So you need to rewrite that function from scratch. Either pass a pointer to allocated memory as a parameter or allocate memory dynamically inside the function. But before you do, I would strongly recommend that you study arrays and pointers some more, for example by reading this chapter of the C FAQ.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • yup. I'm an idiot. Malloc in the tokenize function solved it. Just learning C and used to Java. Good ol Java. – FreeSandwiches Apr 23 '15 at 14:39
  • @ScottCorbett Classic Java-programmer-learning-C bug :) Just remember that C has no garbage collection either, so you will need to `free()` all memory allocated. At any rate I strongly recommend reading everything at http://c-faq.com/, it is excellent reading for beginners and veterans all alike. – Lundin Apr 23 '15 at 14:41