Segmentation fault in home-grown C string manipulation code...
-
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
-
Answer:
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
zefyre-ga at Google Answers Visit the source
Related Q & A:
- How to Convert Code from VB to C++?Best solution by Stack Overflow
- How to split a string in C++?Best solution by Stack Overflow
- What could be the possible reasons for segmentation fault?Best solution by Stack Overflow
- How to convert from string to char in C++?Best solution by Stack Overflow
- How to marshall a string in C?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.