944,015 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 806
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 10th, 2009
0

A whole bunch of Segmentation Faults

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009
Nov 10th, 2009
-7
Re: A whole bunch of Segmentation Faults
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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 10th, 2009
0
Re: A whole bunch of Segmentation Faults
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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009
Nov 10th, 2009
2
Re: A whole bunch of Segmentation Faults
Click to Expand / Collapse  Quote originally posted by thure ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 10th, 2009
0
Re: A whole bunch of Segmentation Faults
I'm never sure when to use malloc and when C allocates the memory for me. Thank you, I'll try that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009
Nov 10th, 2009
2
Re: A whole bunch of Segmentation Faults
Click to Expand / Collapse  Quote originally posted by thure ...
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.

Click to Expand / Collapse  Quote originally posted by thure ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 10th, 2009
0
Re: A whole bunch of Segmentation Faults
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009
Nov 10th, 2009
0
Re: A whole bunch of Segmentation Faults
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009
Nov 10th, 2009
1
Re: A whole bunch of Segmentation Faults
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 11th, 2009
0
Re: A whole bunch of Segmentation Faults
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thure is offline Offline
8 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Error while declaring a structure
Next Thread in C Forum Timeline: delete same array element in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC