Mysterious Seg Fault

This is a discussion on Mysterious Seg Fault within the C Programming forums, part of the General Programming Boards category; Hello all, I've been trying to finish up a function that takes a string and breaks it down according to ...

  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
    Katy, Texas
    Posts
    2,309
    Your string is probably in write protected storage, and is probably being defined like this:

    char mystring[] = "Hi there" ;

    or some such value.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,264
    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
    Katy, Texas
    Posts
    2,309
    Sure there's a valid way.

    Code:
     
    char mystring[81] ;  
    strcpy(mystring,"TEST STRING") ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 02:22 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21