Thread: Very strange behavior possibly involving strtok

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Very strange behavior possibly involving strtok

    I've encountered a very strange issue that may or may not be caused by strtok.

    Let me first just say that I can work around this by writing the program a different way, but at this point I'm just puzzled as to why this is going on.

    Code:
    int main()
    {
    	char* buffer[2] = {0, 0};
    	long size50, size10;
    
    	readFile("50000words.txt", buffer[0], size50);
    	readFile("10000words.txt", buffer[1], size10);
    
    	char **a;
    	
            //entryList and lookupList contain pointers to locations in buffer[0]/[1] respectively.
            char *entryList[numEntries],
    		*lookupList[numLookups];
    	int entryVals[numEntries],
    		lookupVals[numLookups];
    
    	const char delim[] = ", \n\r";
    	
    	int i = 0;
    	entryList[0] = strtok(buffer[0], delim);
    	
    	cout << entryList[0] << endl;	//Tetraheism
    	
    	while(entryList[i] != NULL && i < numEntries)
    	{
    		entryVals[i] = atoi(strtok(NULL, delim));
    		entryList[++i] = strtok(NULL, delim);
    	}
    	
    	cout << entryList[0] << endl;	//Tetraheism
    	cout << entryList[1] << endl;	//Chrisoms
    	
    	
    	lookupList[0] = strtok(buffer[1], delim);
    	
    	cout << entryList[0] << endl;	//Tetraheism
    	cout << entryList[1] << endl;	//Chrisoms
    	
    	i = 0;
    	while(lookupList[i] != NULL && i < numLookups)
    	{
    		lookupList[++i] = strtok(NULL, delim);
    	}
    	
    	cout << entryList[0] << endl;	//chwartzlot  <---- WHAT? this string is towards the end of the buffer.
    	cout << entryList[1] << endl;	//Chrisoms
    So, I'm a bit puzzled as to exactly what could have happened here. Even more so because only pointed-to value of the first element of entryList has changed.

    entryList[0] points to a location in buffer[0]. Casting entryList[0] to void* in the cout statement shows that the address pointed to changes. Can anyone explain why?
    Last edited by rainey; 05-17-2010 at 07:32 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You walk off the end of the lookupList array, by accessing lookupList[numLookups] which doesn't exist. (Since you're doing pre-increment, you get an off-by-one error even though the while loop "looks" right.)

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    Ah jeez, and here I was thinking it was some sort of ridiculous error caused by strtok.

    I've spent too much time spent in the safety of Java. I guess I expected some sort of exception to be thrown by going out of bounds.

    Thanks, fixed up the loops and it works fine now .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Strange error involving rand()
    By Samus Man in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 08:40 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM
  5. strange request involving a CD
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-16-2003, 03:44 PM