Google Answers Logo
View Question
 
Q: Segmentation fault in home-grown C string manipulation code... ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Segmentation fault in home-grown C string manipulation code...
Category: Computers > Programming
Asked by: zefyre-ga
List Price: $10.00
Posted: 04 Feb 2004 10:36 PST
Expires: 05 Mar 2004 10:36 PST
Question ID: 303553
I am working on a string manipulation library in C.  I am trying to
create my own version of the strchr() function, using the following
code:

char *es_strchr(char needle, char * haystack) {
	int count_a = 0;
	int count_b = 0;
	int found_needle = 0;
	char lastchar;
	char * return_value;
	for ( ; ; ) {
		lastchar = haystack[count_a];
		if (lastchar == needle) {
			found_needle = 1;
		}
		if (found_needle == 1) {
			if (lastchar != '\0') {
				return_value[count_b] = lastchar;
				count_b++;
			}
		}
		if (lastchar == '\0') {
			if (found_needle == 1) {
				return_value[count_b] = lastchar;
			}
			break;
		}
		count_a++;
	}
	return(return_value);
}

When I try to test this code by creating a test frontend (let's say,
test.c), and do the following...

char * request = es_strchr(' ', "GET /index.html
HTTP/1.0\r\nUser-Agent: EbertSoft-UICI\r\n\r\n

... I get a "segmentation fault" ... but if I remove the space
character between "User-Agent:" and "EbertSoft-UICI", the code runs
fine.

I need to know what's wrong with my es_strchr() function code.

Thanks for your help!
-- Quinn

Request for Question Clarification by hammer-ga on 04 Feb 2004 10:47 PST
Do you want your code fixed for you, or do you want guidance as to
what's wrong so you can fix it yourself?

You seem to be trying to teach yourself to do something, so I don't
want to just tell you the answer, if that's not what you want.

- Hammer

Clarification of Question by zefyre-ga on 04 Feb 2004 11:09 PST
Hi,

I am trying to learn how to do this as I go along, but, I would prefer
to know what I am doing wrong. I learn best by example.

Thank you,
Quinn
Answer  
Subject: Re: Segmentation fault in home-grown C string manipulation code...
Answered By: hammer-ga on 04 Feb 2004 12:00 PST
Rated:5 out of 5 stars
 
Zefyre,

Your code has several problems. One of which is that it does not
actually do what strchr() does. We'll get to that in a moment.

First, the reason for your crash:
You declare a char pointer called return_value. A pointer has to point
at something. You don't initialize it, and you don't point it at
anything valid. Therefore, it is very likely to crash if you try to
use it. Note that you crash when your code succeeds in matching
needle. This condition allows your code to run the part where you try
to set return_value[count_b]. Since this location is a pointer off
into la-la land, you crash. To use a char pointer this way, you need
to allocate some memory for it to point at. For example, malloc().

However, all of this is actually irrelevant. You don't need any of
this to duplicate strchr. To work like strchr, your code needs to
return a pointer into haystack. The way your code is currently
written, even if you fix the crash, you will simply return the
character you were originally looking for. The point of strchr is to
return a pointer that show where in the original string the character
appears.

MSDN Library Reference for strchr
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strchr.2c_.wcschr.2c_._mbschr.asp

Here is a commented implementation of strchr:

char    *my_strchr(char needle, char *haystack)
{
// Declare some char pointers
char    *return_value;
char    *cp;

        // Initialize return value
        return_value = NULL;
        // Make sure haystack is valid
        if(haystack != NULL)
        {
                // Set pointer to beginning of haystack
                cp = haystack;
                // Run until we either find a match or 
                // get to the end of haystack.
                while((*cp != '\0') && (return_value == NULL))
                {
                        // If we found a match, set return value 
                        // and exit the loop
                        if(*cp == needle) return_value = cp;
                       // Advance cp
                        cp++;
                }
        }
        // If we didn't find a match, return_value is
        // still NULL. Otherwise, it is pointing at the
        // first matching character in haystack.
        return(return_value);
}

In your example above, return_value would be " EbertSoft-UICI".

I hope this clarifies things for you. Please let me know if you need
any of the techniques in the example explained.

Search strategy: None. I've just done this before!

- Hammer

Request for Answer Clarification by zefyre-ga on 04 Feb 2004 12:23 PST
Hammer,

I don't know if it's just because I'm stupid, but, I don't understand
how you can do the following...

char *cp;

.. and then set it to the first character of haystack with ...

cp = haystack;

I would think that it would set cp to be equal to ALL of haystack.

Thanks for the info,
Quinn

Clarification of Answer by hammer-ga on 04 Feb 2004 12:32 PST
You're used to working in a higher level language, like VB, aren't you? *wink*

You are not dealing with String objects here. You are dealing with
pointers into memory. A pointer is a number that represents a memory
address, not a string value. cp = haystack sets cp to point at the
same address as haystack. That address represents the beginning of a
chunk of memory which contains your string value. When cp = haystack,
cp points at the first letter in haystack. When cp = haystack + 1, cp
points at the second letter in haystack, and so on. You have to be
careful with this in C. You can keep doing cp++ and run cp out past
the end of haystack and off into wherever. C won't stop you!

- Hammer

Request for Answer Clarification by zefyre-ga on 04 Feb 2004 12:41 PST
Hammer,

Actually, I am very used to VB, and that probably is what's holding me back. ;-)

Thanks for your help!
-- Quinn

Clarification of Answer by hammer-ga on 04 Feb 2004 12:46 PST
You are welcome.

BTW, you may also want to note that, when comparing the characters, I use 
if(*cp == needle).

The * in front of cp means, the value at the address pointed to by cp.
This returns the character itself instead of the memory address of the
character.

- Hammer
zefyre-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
Thanks for being such a great help!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy