Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i write this code and it works well but when i call parsing more times it cases stack overflow
how can i optimize parsing function
C#
char raw_data2[] = "-------------------------------------------------------------------------------/"
"Testing NO.         : 1/"
"Beginning Time      : 2014-02-13 13:34:36/"
"Total Lines to test : 1/"
"Testing Result      :    1/"

"Status: finished/"

 "------------Accomplished testing lines and items------------/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
    "SLN(unit-sunit-index)           TESTITEM        RESULT/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"

   "1-03-01  LineVolt  A-B   DC Vol:  019.11V  AC Vol: 0000.20V/"

   "1-03-01  LineVolt  A-GND DC Vol: -025.00V  AC Vol:0000.50V/"

   "1-03-01  LineVolt  B-GND DC Vol: -044.12V  AC Vol: 0000.56V/"

 "-----------No-accomplished testing lines and items-----------/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
    "SLN(unit-sunit-index)                 TESTITEM/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
"Total tested count  : 1 /"
"Total success count : 1 /"
"Total failure count : 0 /"
"---------------------------   END  ------------------------------------------";
        //char h [4];
        //delete []h;
        data = parsing(raw_data3,"A-B   DC Vol",":");
        data = parsing(raw_data3,"DC Vol",":",1);
        data = parsing(raw_data3,"A-B   DC Vo",":",2);
        float jk = atof(data.c_str());
        data = parsing(raw_data3,"Insulation capacitor  A-B",":");
        data = parsing(raw_data3,"Insulation resistence A-B",":");
        data = parsing(raw_data3,"A-B   DC Vol",":");
}
string parsing(string raw_data,string search_str,string delm , int offset)
{
	int  gg = raw_data.find(search_str);
	while(offset != 0) 
	{
	raw_data = raw_data.substr(gg+search_str.length());
	offset--;
	}
	trim(search_str);
	if(raw_data.length() == 0)
		return "";
	
	char *g;// = new char[50];//="";
	g = strtok((char*)raw_data.c_str(),delm.c_str());
	char value[50];
	memset(value,0,sizeof(value));
	int index = 0;
	bool trim_first =false;
	while(true)
	{
		if(!strstr(g,search_str.c_str())) 
		g =strtok(NULL,delm.c_str());
				if(g==NULL)
				               return "error5";
	
		
//		raw_data+=strlen(g);
		else if(strstr(g,search_str.c_str()))
		{ 
			g = strtok(NULL,delm.c_str());
			while (true)
			{
				while(!trim_first)
				{
							if(g==NULL)
				               return "error";
	
					// trim white space
					if(g[0]==' ')
					{
						g++;
				    }
					else
					{
						trim_first = true;
						break;
					}
				}
				if(g[0]==' ' || g[0]=='\n' || g[0]=='\r' ||g[0]==NULL)
					break;
				else 
				{
					value[index] = g[0];
					index++;
					g++;
				}
				
				
			}
	  	
			break;
		}
	}
	
	//delete[] g;
	return string(value);
}
  void trim(std::string& str)
{
	
str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
//return str;
}
Posted
Comments
Richard MacCutchan 10-May-14 8:32am    
The first thing to do is to use your debugger to find out where the stack overflow occurs, and correct the code so it no longer happens.
CPallini 10-May-14 8:51am    
Please provide a the exact scenario making your function fail.
Mahmoud_Gamal 10-May-14 11:39am    
OK for example when i call parsing function for first time and second it work correctly but when i call third times it case stack over flow but when i work on high ram it work perfectly
Sergey Alexandrovich Kryukov 11-May-14 21:31pm    
Why would you "optimize" something which does not work at all? Don't you want to fix the bugs? :-)
—SA
Legor 12-May-14 4:23am    
You code doesnt "work well" if it causes a stack overflow.

1 solution

value is declared to be 50 characters but your index variable is unbounded. value is declared on the stack so once index reaches 50, you will start to erase other data on the stack.

As a side note, you can replace:

C++
char value[50];
memset(value,0,sizeof(value));


... with this ...

C++
char value[50] = {0};


Also, I suspect your sample data is missing newlines ("\n") from the end of each line.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900