Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Segmentation fault

    Hello,
    I need a little help.
    This code works without any errors.
    Code:
    int main()
    {
    	int i=0;
            char c=0;
    	char *command;
    
    	while ((c=getchar()) != '\n'){  
    	    command[i]=c;
    	    i++;
    	 }
    
    	command[i]='\0';
    	return 1;
    }
    
    However, when I add a new variable, for example:
    
    int main()
    {
    	int i=0, k=0;
            char c=0;
    	char *command;
    
    	while ((c=getchar()) != '\n'){  
    	    command[i]=c;
    	    i++;
    	 }
    
    	command[i]='\0';
    	printf(" k is %d\n", k+3);[/B]
    	return 1;
    }
    the codes stop working and I get a "Segmentation fault" announcement.
    What is the problem?

    Thanks
    Last edited by Salem; 04-27-2010 at 09:52 AM. Reason: [code][/code] tags - learn to use them yourself!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, in both cases, command is a pointer to char that does not point to anything in particular, yet you write command[i] = c;

    Incidentally, c should be an int instead of a char since getchar() returns an int (as is necessary to compare with EOF, should you choose to do so).

    Oh, and please post code in [code][/code] bbcode tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I've change c type to int, but I still got segmentation fault (and I'v change to compare with EOF)

    And moreover, it stopped working without the new variable.
    Last edited by ksusha; 04-27-2010 at 09:52 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, but that does not address the first part: command does not point to anything. Change it to be an array instead, then check that i is always a valid index before you do command[i].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *command;
    Where does this point?

    You've written
    char *command = somerandomaddressinmemory;

    Rearrange the code, you're walking in a different part of the mine-field.
    The failure to blow up doesn't make it any less of a minefield.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    The problem is that my input is of unknown length, and that's why I can't use an array

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ksusha
    The problem is that my input is of unknown length, and that's why I can't use an array
    In that case, use malloc(), realloc() and free(). You would keep track of both the current size of the dynamic array (as in the number of elements in use) and its capacity. The former is already done with i, so you need another variable for the latter. When the size is about to exceed capacity, you "expand" the dynamic array with realloc().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Ok, I'll try.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM