Thread: Mysterious Seg Fault

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Mysterious Seg Fault

    Hello all, I've been trying to finish up a function that takes a string and breaks it down according to the whitespaces. It then saves it into a vector array (array of strings with a terminating NULL) and returns it. For some reason, when I attempt to change a whitespace into a '\0', I get a seg fault. I'm all out of ideas, so any help is much appreciated!

    Code:
    char **my_str2vect(char* string)
    {
      char** vect;
      char* temp;
      int num_white = 0;
    
      temp = string;
    
      for(; *temp!= '\0'; temp++)
      {
        if(*temp == ' ')
        {
          num_white++; //Counts the number of whitespaces
        }
      }
    
      vect = (char**) xmalloc((num_white+1) *sizeof(char*)); //Allocates space for the char* array
    
      *vect = string; //Pointer to first string is saved at vect[0]
    
      for(; *string != '\0'; string++)
      {
        if(*string == ' ')
        {
          *string = '\0'; //This is where I get seg faults
          *vect = ++string;
          vect++;
        }
      }
    
      *vect = NULL;
    
      return vect;
    }
    -It seg faults after encountering "*string = '\0'" on the first time.
    -xmalloc() is the same as malloc()

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your string is probably in write protected storage, and is probably being defined like this:

    char mystring[] = "Hi there" ;

    or some such value.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dino View Post
    Your string is probably in write protected storage, and is probably being defined like this:
    Sounds likely thus far. But...
    Quote Originally Posted by Dino View Post
    char mystring[] = "Hi there" ;

    or some such value.
    Is more likely to be:
    Code:
    char *stringliteral = "youcantchangethis";
    char arraywithunspecifiedsize[] = "youcanchangethisifyoudonotrunoutofbounds";
    It's probably the former. It could be that they're running off the end of an array that they think it a string. But it would be hard for them to encounter that if they were using the latter as shown above. Even if they were using isspace, it would find the nul character.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, it may also look similar to this:
    Code:
    my_str2vect("some string");
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Yea, that's how I tried to test it.

    Code:
    char* string = "TEST STRING";
    char** cmd = my_str2vect(string);
    So I guess that's not a valid way to test my code?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sure there's a valid way.

    Code:
     
    char mystring[81] ;  
    strcpy(mystring,"TEST STRING") ;
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Thanks, fixed it all. Just fighting with execvp() right now, but I'll have it done soon enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf seg fault
    By mercuryfrost in forum C Programming
    Replies: 19
    Last Post: 08-20-2009, 01:22 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM