CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Exclamation strstr() Runtime Error - HELP!

    i have made this strstr() function,it gives the correct answer if it finds the correct string inside the another but the program crashes if it does not find the string,gives a runtime error,compliler does not give any error!

    here is my code:
    Code:
    char *astrstr(const char *s1,const  char *s2)
    {
    	char *p=NULL;
    
    	int x=astrlen(s1);
    	int y=astrlen(s2);
    
    	int o=0,z=0;
    	for(int i=0;i<x; i++)
    	{
    		for(int k=0;k<y;k++)
    		{
    			if(s1[i]==s2[k])
    				{	o=0;z=0;	
    						for(int l=i;l<x;l++)
    						{
    							if(s1[o+i]==s2[o])
    							{
    								z++;
    							}
    							o++;
    						}
    			
    			
    				}
    				
    				if(z==y)
    					{
    						return (char *)(s1+i);
    				
    					}
    		}
    	}
    return p;
    	
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char s[]={"this is a simple string"};
    	char t[]={"is"};
    	cout << " S: "<< s << endl << " T: "<< t << endl << endl;
    	
    	cout  << "My Function: " << endl <<  astrstr(s,t) << endl << " String Function : " << endl << strstr(s,t) << endl  << "S: "<< s << endl << "T: "<< t << endl;
    	
    	
    	
    	return 0;
    }
    Last edited by arshad115; March 10th, 2009 at 09:18 AM. Reason: forgot the code tags!...;p

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: strstr() Runtime Error - HELP!

    Debug your code step by step to see where this error happens and what are the values of all your variables in this moment.
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: strstr() Runtime Error - HELP!

    One problem you have is in this section
    Code:
    for(int l=i;l<x;l++)
    {
    	if(s1[o+i]==s2[o])
    	{
    		z++;
    	}
    	o++;
    }
    There's no constraint on o, so it walks right of the end of s2.

  4. #4
    Join Date
    Mar 2009
    Posts
    20

    Re: strstr() Runtime Error - HELP!

    i debugged my code and it gives the following error just as it calls my function

    "Unhandled exception at 0x1026f8e0 in Assignment 3_Question 2.exe: 0xC0000005: Access violation reading location 0x00000000."

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: strstr() Runtime Error - HELP!

    Quote Originally Posted by arshad115 View Post
    i debugged my code and it gives the following error just as it calls my function

    "Unhandled exception at 0x1026f8e0 in Assignment 3_Question 2.exe: 0xC0000005: Access violation reading location 0x00000000."
    There's a little more to debugging than that. You need to look at the line of code that caused the error, what values are in the variables and when appropriate, the call stack. You can also watch your program run line by line, watching the variables as they change. That's what I did to see o running off the end of s2.

    I showed you where I believe the error is coming from.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: strstr() Runtime Error - HELP!

    A null pointer access probably wouldn't come from an array overrun. Both are problems, but probably different ones.

    What's bugging me is the use of {} here:
    Code:
    	char s[]={"this is a simple string"};
    	char t[]={"is"};
    I'm not sure if it's actually wrong, but it is strange.
    Last edited by Lindley; March 10th, 2009 at 10:38 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: strstr() Runtime Error - HELP!

    Quote Originally Posted by Lindley View Post
    A null pointer access probably wouldn't come from an array overrun. Both are problems, but probably different ones.
    It's not crashing at the overrun, but it's symptomatic of faults in his logic that are causing him to set an invalid pointer downstream.

  8. #8
    Join Date
    Mar 2009
    Posts
    20

    Re: strstr() Runtime Error - HELP!

    Code:
    char s[]={"this is a simple string"};
    	char t[]={"is"};
    is used to to initialize the array by the string given,the comipler counts the number automatically

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: strstr() Runtime Error - HELP!

    Quote Originally Posted by arshad115 View Post
    Code:
    char s[]={"this is a simple string"};
    	char t[]={"is"};
    is used to to initialize the array by the string given,the comipler counts the number automatically
    Then why do you use {}?
    Code:
    char s[] = "this is a simple string";
    char t[] = "is";
    Victor Nijegorodov

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: strstr() Runtime Error - HELP!

    Normally you'd just write
    Code:
    char s[]="this is a simple string";
    char t[]="is";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured