views:

247

answers:

2
while(1){
    //Command prompt
    char *command;
    printf("%s>",current_working_directory);
    scanf("%s",command);<--seg faults after input has been received.
    printf("\ncommand:%s\n",command);
}

I am getting a few different errors and they don't really seem reproducible (except for the segfault at this point >.<). This code worked fine about 10 minutes ago, then it infinite looped the printf command and now it seg faults on the line mentioned above. The only thing I changed was scanf(">%s",command); to what it currently is. If I change the command variable to be an array it works; obviously this is because the storage is set aside for it.

  1. I got prosecuted about telling someone that they needed to malloc a pointer* (but that usually seems to solve the problem such as making it an array)
  2. The command I am entering is "magic" -- 5 characters -- so there shouldn't be any crazy stack overflow.
  3. I am running on Mac OS X 10.6 with newest version of xCode (non-OS4) and standard gcc
  4. This is how I compile the program: gcc --std=c99 -W sfs.c

Just trying to figure out what is going on. Since this is for a school project I am never going to have to see again, I will just code some noob work around that would make my boss cry :) But for afterwards I would love to figure out why this is happening and not just make some fix for it, and if there is some fix for it why that fix works.

+10  A: 

scanf tries to store the data it reads into the argument (command in your case). That variable has not been initialized to point to valid memory. The malloc to assign memory to it would, therefore, make it valid. It could also be declared on the stack:

char command[somearraysize];
Mark Wilkins
Yeah I thought that by using char *command, that the input would be placed on the stack. I run into this problem more often than not and end up using malloc and free to the point of abuse to keep my program from seg faulting. I graduate in 3 weeks and I don't need to lose points to silly seg-faults :)
Shadow
These are very basic concepts in C. when you pass 'command' to the scanf function, scanf cannot make that pointer point somewhere, nor can C magically allocate space on the stack ang have the pointer point there. You need to supply the storage.
leeeroy
@Shadow: The variable named command is (typically) on the stack. But it is a pointer with a potentially "random" value until you initialize it. scanf would try to write data to that "random" address.
Mark Wilkins
Thanks, you were a lot of help :)
Shadow
+9  A: 
 char command[100];
 scanf("%s",command);

As to why this is necessary, I suggest reading a book on C, such as The C Programming Language.

anon
I understand creating a buffer. I work in industry and I am a graduating senior but thanks for the tip >.>
Shadow
~Shadow No, you don't understand.
anon
Wow thanks a lot. Not even going to try and be helpful other than point me to references I have already used. I know how that works, I wanted to know why mine didn't. If things are placed on the stack by using char *command why is that seg faulting, other than walking in memory that is shouldn't be. But that shouldn't happen because the program has it's own stack.
Shadow
@Shadow: `char* command;` allocates only an uninitialized pointer on the stack. Then your `scanf` call uses that pointer to put the results somewhere. Is that clearer?
Fred Larson
John Saunders
@Neil, @John: you're both technically right, but I suspect that your responses read too acerbically to actually have your point be received well. If you're going to take the trouble to respond, wouldn't you rather have the person heed your advice?
Beska
Meta discussion about this answer: http://meta.stackoverflow.com/questions/46494/haterade-being-poured-on-questions (FWIW, I upvoted both this answer and Shadow's question about it).
Ether