Thread: Call StrTok() second time caused Access Violation Reading

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Call StrTok() second time caused Access Violation Reading

    Hi Everyone,

    I am coding StrTok() which functioned as strtok predefine in cpp library. When it called this function second time and I got Access violation Error. Please see this piece of code.
    Code:
    char* StrTok1(char* strToken, const char* strDelimit)
    {
    	static char* strTokenCopy;
    	static char* CurDelimitPos;
    	char* CurToken = new char();//reset it every time
    	char* ReturnCurToken = CurToken;//always points to CurToken
    	
    	if(strToken != NULL)
    	{
    		strTokenCopy = new char;
    		//copy all elements
    		for(char* strTokenPtr = strTokenCopy ; *strToken; *strToken++)
    		{
    			*strTokenPtr++ = *strToken;
    		}
    		*strTokenPtr = NULL;
    		CurDelimitPos = strTokenCopy;//set CurDelimitPos points to first char of strTokenCopy
    	}
    	else
    	{
    	
    		
    		//advance to next position in stkTokenCopy
    		*CurDelimitPos++;
    	}
    	
    	for(char* Token = CurDelimitPos; *Token; *Token++)
    	{
    		const char* Delimit;
    		
    		//check if Token matchs any Delimit
    		for(Delimit = strDelimit; *Delimit;)//Delimit looks in strDelimit
    		{
    			if(*Token != *Delimit)
    				*Delimit++;
    			else//found Token in strDelimit
    			{
    				*CurToken = NULL;
    				CurDelimitPos = Token;	
    				break;
    			}			
    				
    		}//end for
    		if(*Token == *Delimit)
    		{
    			break;//get out Token for loop
    		}
    		if(*Delimit == NULL)
    		{
    			*CurToken++ = *Token;//copy Token to CurToken
    		}
    	}//end for
    	
    	
    	return ReturnCurToken;
    	
    		
    	
    }
    void main(void)
    {
    	char string[] = "A string\tof ,,tokens\nand some more tokens";
    	char seps[] = " ,\t\n";
    	char* token;
    
    	cout<<"Tokens:\n";
    	token = StrTok1(string, seps);
    	cout<<"\""<<token<<"\""<<endl;
    	token = StrTok1( NULL, seps);
    	cout<<"\""<<token<<"\""<<endl;
    	token = StrTok1( NULL, seps);
    	cout<<"\""<<token<<"\""<<endl;
    
    }
    When I debugged it and it showed this "char* CurToken = new char();" gave me Access Violation Error. I don't see anything wrong with it.
    Thanks in advance.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Code:
    char* CurToken = new char();
    strTokenCopy = new char;
    Ok, on this first line of code, is there a reason for the parenthesis?
    Second, when you new a variable, you need to delete it when you are done with it. Try removing the parenthesis on that first line of code, and at the end of the function, add this:
    Code:
    delete CurToken;
    delete strTokenCopy;
    On a side note: I didn't take a good look at your code, but does your StrTok1() function do anything different than strtok()? What is the purpose of it? There may be a better way to accomplish what you need.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    Thank for your comment.
    I should not delete CurToken and strTokenCopy. CurToken is return by the function and strTokenCopy is static char pointer and its value is needed in every call.
    To answer on side notes: this StrTok1 is working exactly as the original strtok().
    Again any idea on why it gave me an error?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char* CurToken = new char();//reset it every time
    This allocates room for one char.

    > strTokenCopy = new char;
    Same as above, this only allocates room for a single char.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by brucejunor
    I should not delete CurToken and strTokenCopy. CurToken is return by the function and strTokenCopy is static char pointer and its value is needed in every call.
    You still should delete those variables. Anything you initialize with new should be freed with delete, else you get memory leaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  3. Access Violation Of Memory Buffer WHY??
    By bartybasher in forum Game Programming
    Replies: 20
    Last Post: 08-13-2004, 07:33 AM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM