Originally Posted by
laserlight
I note that you did not allocate any space for the copy.
Excellent find!
I was just about to post a new thread asking why the tokenizer function returns a SIGSEGV error. So then I did:
Code:
char* tokenizer (string inputstream)
{
char* cstringcpy;
const char* cstr = inputstream.c_str();
strcpy(cstringcpy,cstr);
char* wordptr = (char*)malloc (50 * sizeof(char));
wordptr = strtok(cstringcpy,"\t");
return wordptr;
}
Even though I malloced wordptr, I STILL got a SIGSEGV error even though I passed wordptr on the heap.
Then I realised the thing that wordptr pointed to is in itself was not on the heap. So then I finally did a change on line 3 to allocate the COPY in memory:
Code:
char* tokenizer (string inputstream)
{
char* cstringcpy = (char*)malloc (50 * sizeof(char));
const char* cstr = inputstream.c_str();
strcpy(cstringcpy,cstr);
char* wordptr = (char*)malloc (50 * sizeof(char));
wordptr = strtok(cstringcpy,"\t");
return wordptr;
}
And I finally get no error :-) Is this why you pointed to the dangers of not assigning memory to the copy?