tags:

views:

140

answers:

6
char *substring(char *text, int position, int length)
{
   int i, j=0;
   char *temp ;

   for(i=position-1; i<position+length-1; i++)
   {
     temp[j++] = text[i];
   }
   temp[j] = '\0';

   return temp;
}

Hi What is the error in the following code.. I am trying to run this on Fedora Machine.. And its giving me a run-time error "Segmentation Fault". What is this error all about.. and why is it giving this error..

Thanks..

+6  A: 

temp is uninitialized.

Daniel Stutzbach
+2  A: 

It means that your code has violated some restriction set up by the operating system, in this case you are writing to memory that you do not have the right to write to.

This is because your temp variable is just an uninitialized pointer, it doesn't contain the address of memory where you are allowed to write.

If you expect to write length + 1 characters, it must be pointing to at least that many bytes worth of space.

Since you expect to return the string, you need to either make it static (but that can be dangerous), or allocate the space dynamically:

if((temp = malloc(length + 1)) == NULL)
  return NULL;
unwind
+2  A: 

You need to allocate memory for temp - currently it's just a dangling pointer. You can use malloc for this but note that the caller will need to ensure that this storage is subsequently freed.

For example:

char *substring(const char *text, int position, int length)
{
   char *temp = malloc(length + 1);
   int i, j;

   for (i = position, j = 0; i < position + length; i++, j++)
   {
       temp[j] = text[i];
   }
   temp[j] = '\0';

   return temp;
}
Paul R
How can i free the memory here,, as the same pointer is being returned to other function...
AGeek
But here one must be careful to free memory what we have allocated for temp. The caller should take additional responsibility.
dicaprio
@RBA : Ideally the caller should be responsible to free the memory. other way, you may also pass another parameter to this function say char *pcSubStr " which IN-OUT and will return the substring. In this way you would be able to free in the calling function where you need it.
dicaprio
@RBA: ideally the caller should pass in the storage for the output string, and take responsibility for freeing it. In this case though your API as defined prohibits this, so you need to ensure that the caller frees the result string when you're done with it, otherwise you'll have a `memory leak`.
Paul R
A: 

while the answer is obvious - i.e temp is not initialized, here is a suggetion.

If your intention is to find a substring in another string, few alternatives are,

1. use C strstr(...)
2. Robin-Karp method
3. Knuth-Morris-Pratt method
4. Boyer Moore method

Update: Initialy I thought this question was related to finding the substring (based on the title). Anyway, this looks like strchr() implementation.

SysAdmin
I am making a copy of the sub-string into another pointer, this is the just the simple way of finding one substring of a given string..Hope i am that very simple way in the correct manner..Also, The methods given by SysAdmin,, looks pretty complex ones, but still thanx for the suggestion.. I will try and learn those as well.. But if you can tell me whether i have implemented the very basic pattern searching algorithm correctly, then it would be very kind..Thanks..
AGeek
I think the OP's intention is not "find" as in *search*, but "find" as in *get* or *return*. This use of the word "find" is uncommon in English and not generally used in a programming context.
Greg Hewgill
A: 

I am making a copy of the sub-string into another pointer, this is the just the simple way of finding one substring of a given string..

Hope i am that very simple way in the correct manner..

Also, The methods given by SysAdmin,, looks pretty complex ones, but still thanx for the suggestion.. I will try and learn those as well.. But if you can tell me whether i have implemented the very basic pattern searching algorithm correctly, then it would be very kind..

Thanks..

AGeek
A: 

It is obvious from the code that you missed to allocate / initialize the pointer *temp. It is pointing to nowhere.

You either have to use malloc or strdup and do the rest. But yeah , you may also want to explore using strncpy (null terminate) to simplify the code.

dicaprio