A whole bunch of Segmentation Faults

Reply

Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster

A whole bunch of Segmentation Faults

 
0
  #1
15 Days Ago
Hi there,

This is my first time working with an unsafe language, so naturally my first code would be full of memory-related bugs. I still have no idea how I ought to approach some of the problems I've been getting, and I've been resisting asking for help as much as I could.

The assignment I've been given is a buttload of work, so I'd rather not bog anyone down with too much of it. At the moment, I'm trying to work out how to design a menu() function that gives main() both the function choice the user wants and the arguments for that function. Here's what I've got so far:

  1. struct REPLY {
  2. char use[256];
  3. char pass[256];
  4. char type[64];
  5. char nuse[256];
  6. char npass[256];
  7. char ntype[64];
  8. int option;
  9. };
  10.  
  11. struct REPLY *reply;
  12.  
  13. int menu (void)
  14. {
  15. int o;
  16. printf("Please type a number corresponding to the following options:\n");
  17. printf("1. Add\n");
  18. printf("2. Delete\n");
  19. printf("3. Edit\n");
  20. printf("4. Purge\n");
  21. printf("5. Quit\n");
  22. scanf("Selection: %i\n", o);
  23. reply->option=o;
  24. printf("\n\n%i\n\n\n", reply->option);
  25. switch(reply->option){
  26. case '1':
  27. {
  28. printf("Please provide the username.\n");
  29. scanf("%s\n", &reply->use);
  30. printf("Please provide the password.\n");
  31. scanf("%s\n", &reply->pass);
  32. printf("Please provide the user type.\n");
  33. scanf("%s\n", &reply->type);
  34. break;
  35. }
  36. case '2':
  37. {
  38. printf("Please provide the username.\n");
  39. scanf("%s\n",&reply->use);
  40. break;
  41. }
  42. case '3':
  43. {
  44. printf("Please provide the username.\n");
  45. scanf("%s\n",&reply->use);
  46. printf("Please provide the password.\n");
  47. scanf("%s\n",&reply->use);
  48. printf("Please provide the new username.\n");
  49. scanf("%s\n",&reply->nuse);
  50. printf("Please provide the new password.\n");
  51. scanf("%s\n",&reply->npass);
  52. printf("Please provide the new user type.\n");
  53. scanf("%s\n",&reply->ntype);
  54. break;
  55. }
  56. case '4':
  57. {
  58. break;
  59. }
  60. case '5':
  61. {
  62. break;
  63. }
  64. }
  65.  
  66. return 0;
  67. }

This code is saved as menu.c, which is included in the inclusion statements of the main function. I'm getting a segmentation fault at
  1. reply->option=o;
and I haven't been able to find out why. I also get the sense main won't be able to access reply for some reason.

I just need a push, and it's probably so simple it went over my head, but can someone help me out?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,356
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
15 Days Ago
line 22: >> scanf("Selection: %i\n", o);

You can't combine scanf() and printf() like that, and the parameter must be a pointer to an integer
  1. printf("Selction: ");
  2. scanf("%i", &o);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster
 
0
  #3
15 Days Ago
That dismissed one of the warnings, but I still get a segmentation fault at
  1. reply->option=o;
Why is that even a problem?

All of the other warnings look like this:
  1. menu.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[256]
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
2
  #4
15 Days Ago
Originally Posted by thure View Post
That dismissed one of the warnings, but I still get a segmentation fault at
  1. reply->option=o;
Why is that even a problem?
struct REPLY *reply; is a pointer, anything you try to write to it it will produce a segmentation fault, since there's no memory allocated for it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster
 
0
  #5
15 Days Ago
I'm never sure when to use malloc and when C allocates the memory for me. Thank you, I'll try that.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
2
  #6
15 Days Ago
Originally Posted by thure View Post
I'm never sure when to use malloc and when C allocates the memory for me.
For some, memory management in C, is a curse, nevertheless it is one of the strength of the language.

Originally Posted by thure View Post
All of the other warnings look like this:
  1. menu.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[256]
After allocating memory for it, remove any & in front of those pointers to string like
scanf("%s\n", &reply->pass);
and the '\n' is not going to give you the intended result, neither.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster
 
0
  #7
15 Days Ago
Success! This was the sort of push I needed. I'm not getting any warnings, and as far as I can tell the menu is working properly.

I might have questions later about how to get menu()'s reply back to main() since C's scoping is so complex. This is what I've done:
  1. ...
  2.  
  3. struct REPL *reply;
  4.  
  5. int menu (void)
  6. {
  7. reply=(struct REPL*)malloc(sizeof(struct REPL));
  8.  
  9. ...

Since reply is declared globally, will main() be able to see and use it after menu is finished?

@Aia, thanks for your help - I really appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster
 
0
  #8
15 Days Ago
Alright, more segmentation faults...

This time I'm trying to parse a file into a linked list. The intent is to decrypt and read the file line-by-line, separating a line into parts and storing those parts into a struct.

I'm only getting the segmentation fault when the file, password.csv, already exists, for some reason. If it's starting anew the program behaves normally.

GDB says the problem is in this line. The whole function is pasted below.
  1. while(fgets(c, 600, fin)!=NULL)

It happens in this function:
  1. int parse (void){
  2. cipher(0);
  3. FILE *fin=fopen("password.csv", "rt");
  4. //processing
  5. char *c;
  6. while(fgets(c, 600, fin)!=NULL){
  7. char use[256];
  8. char pass[256];
  9. char type[64];
  10. strcpy(use, strtok(c,", "));
  11. strcpy(pass, strtok(NULL,", "));
  12. strcpy(type, strtok(NULL,", "));
  13. push(use, pass, type);
  14. }
  15. fclose(fin);
  16. return 1;
  17. }

I'd really appreciate your thoughts.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #9
15 Days Ago
char *c; is a pointer, again, it doesn't point to any proper memory allocated to it.
while(fgets(c, 600, fin)!=NULL) Guess what you are trying to do there? Yeap! Trying to write to poor char *c that it doesn't have any proper memory available for it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: thure is an unknown quantity at this point 
Solved Threads: 0
thure thure is offline Offline
Newbie Poster
 
0
  #10
15 Days Ago
Aw man, I thought C handled chars! It's just one byte! Okay, thanks again for your help.

As a general rule, do segmentation faults happen because memory isn't allocated? I'd guess with beginner C it wouldn't be anything fancier.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC