By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,132 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,132 IT Pros & Developers. It's quick & easy.

Segmentation Fault - No Idea Why Tried the Little I know

P: 80
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on an Ubuntu Linux computer and am compiling using gcc. Basically what the program is supposed to do is first using "scanf()" it will read in two values from the user into variables, and those variables will then be passed to a method which will use those variables to issue a "system()" function command which will use the arguments accepted from the user to issue an "mplayer" command to the command line. I have tried all that I know to fix the problem (which is basically substituting "%c" for "%s") but to be honest I really have absolutely no idea why I am getting a Segmentation Fault when I run this program(the error occurs after I have read in both values from the user, so I guess I could be passing to the method incorrectly, but really I just don't know). Below is the code for the program I have been talking about, if you can spot what is causing the segmentation fault and explain it to me and if possible how I can correct it I would really appreciate it, I look forward to participating in this forum.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. int main()
  8. {
  9.         char *source;
  10.         char *param;
  11.         printf("Please Enter a Source URL to be recorded\n");
  12.         sscanf("%s",&source);
  13.         printf("\nPlease Enter a file to hold the datastream from the URL\n");
  14.         sscanf("%s",&param);
  15.         getparameters(source, param);
  16.         return 0;
  17. }
  18. int getparameters(char *url, char *outfile)
  19. {
  20.         char *tempurl = url;
  21.         char *tempoutfile = outfile;
  22.         char *mplayer1 = "mplayer -dumpstream ";
  23.         char *mplayer2 = " -dumpfile ";
  24.         char *mplayexec;
  25.         sprintf(mplayexec,"%c%c%c%c",mplayer1,tempurl,mplayer2,tempoutfile);
  26.         printf("\n");
  27.         system(mplayerexec);
  28.         return 0;
  29. }
  30.  
Dec 29 '06 #1
Share this Question
Share on Google+
14 Replies


100+
P: 145
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on an Ubuntu Linux computer and am compiling using gcc. Basically what the program is supposed to do is first using "scanf()" it will read in two values from the user into variables, and those variables will then be passed to a method which will use those variables to issue a "system()" function command which will use the arguments accepted from the user to issue an "mplayer" command to the command line. I have tried all that I know to fix the problem (which is basically substituting "%c" for "%s") but to be honest I really have absolutely no idea why I am getting a Segmentation Fault when I run this program(the error occurs after I have read in both values from the user, so I guess I could be passing to the method incorrectly, but really I just don't know). Below is the code for the program I have been talking about, if you can spot what is causing the segmentation fault and explain it to me and if possible how I can correct it I would really appreciate it, I look forward to participating in this forum.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. int main()
  8. {
  9.         char *source;
  10.         char *param;
  11.         printf("Please Enter a Source URL to be recorded\n");
  12.         sscanf("%s",&source);
  13.         printf("\nPlease Enter a file to hold the datastream from the URL\n");
  14.         sscanf("%s",&param);
  15.         getparameters(source, param);
  16.         return 0;
  17. }
  18. int getparameters(char *url, char *outfile)
  19. {
  20.         char *tempurl = url;
  21.         char *tempoutfile = outfile;
  22.         char *mplayer1 = "mplayer -dumpstream ";
  23.         char *mplayer2 = " -dumpfile ";
  24.         char *mplayexec;
  25.         sprintf(mplayexec,"%c%c%c%c",mplayer1,tempurl,mplayer2,tempoutfile);
  26.         printf("\n");
  27.         system(mplayerexec);
  28.         return 0;
  29. }
  30.  
You need to allocate memory for the following: source, param, mplayexec. e.g.,

Expand|Select|Wrap|Line Numbers
  1. char* source;
  2. source = new char [1024];
  3.  
That should probably do it. -- Paul
Dec 29 '06 #2

P: 80
Well, I tried allocating memory for each of the variables as Paul suggested, but now I get the following compiler errors:

Expand|Select|Wrap|Line Numbers
  1. mplayerschedule.c: In function ‘getparameters’:
  2. mplayerschedule.c:25: error: incompatible types in assignment
  3. mplayerschedule.c:32: error: incompatible types in assignment
  4. mplayerschedule.c:36: error: incompatible types in assignment
  5. mplayerschedule.c:39: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type
I again, have no idea why these error codes are being generated, as I do not see what is wrong with any of the declarations I have made (even at the specified lines) or how I am passing incompatible data to sprintf, could someone please review my revised code and see where I went wrong? Thanks.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. int main()
  8. {
  9.     char* source[1024];
  10.     //source = new char* [1024];
  11.     char* param[1024];
  12.     //param = new char* [1024];
  13.     printf("Please Enter a Source URL to be recorded\n");
  14.     scanf("%s",&source);
  15.     printf("\nPlease Enter a file to hold the datastream from the URL\n");
  16.     scanf("%s",&param);
  17.     getparameters(source, param);
  18.     return 0;
  19. }
  20. int getparameters(char* url[1024], char* outfile[1024])
  21. {
  22.     //char *tempurl = url;
  23.     char* tempurl[1024];
  24.     //tempurl = new char* [1024];
  25.     tempurl = url;
  26.     //char *tempoutfile = outfile;
  27.     char* tempoutfile[1024];
  28.     //tempoutfile = new char* [1024];
  29.     //char *mplayer1 = "mplayer -dumpstream ";
  30.     char* mplayer1[1024];
  31.     //mplayer1 = new char* [1024];
  32.     mplayer1 = "mplayer -dumpstream ";
  33.     //char *mplayer2 = " -dumpfile ";
  34.     char* mplayer2[1024];
  35.     //mplayer2 = new char* [1024];
  36.     mplayer2 = " -dumpfile ";
  37.     char* mplayexec[1024];
  38.     //mplayerexec = new char* [1024];
  39.     sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  40.     printf("\n");
  41.     system(mplayexec);
  42.     return 0;
  43. }
  44.  
  45.  
Dec 29 '06 #3

100+
P: 145
Well, I tried allocating memory for each of the variables as Paul suggested, but now I get the following compiler errors *snip*
Expand|Select|Wrap|Line Numbers
  1.     char* source[1024];
  2.     //source = new char* [1024];
  3.  
What you've done here is allocate 1024 memory addresses of characters. What you really want is 1024 characters. You need to do it like this (in two steps):

Expand|Select|Wrap|Line Numbers
  1. char* source;
  2. source = new char [1024];
  3. char* param;
  4. param =new char [1024];
  5.  
and so forth.

Also, this is perfectly fine:
Expand|Select|Wrap|Line Numbers
  1. char* blah = "blah blah blah blah";
  2.  
That's why I only flagged certain char* parts for needing allocation. (Although I may have missed some.)

Lastly, nice work on switching from %c to %s in your sprintf line toward the end. Keep at it; you're almost there! :) -- Paul
Dec 29 '06 #4

P: 80
Forgive me if I am wrong, but isn't this object oriented C++ syntax? I am using C only.
Expand|Select|Wrap|Line Numbers
  1. char* tempoutfile;
  2.     tempoutfile = new char [1024];
When I do as you suggest I get the following compiler errors:
Expand|Select|Wrap|Line Numbers
  1. mplayerschedule.c: In function ‘main’:
  2. mplayerschedule.c:10: error: ‘new’ undeclared (first use in this function)
  3. mplayerschedule.c:10: error: (Each undeclared identifier is reported only once
  4. mplayerschedule.c:10: error: for each function it appears in.)
  5. mplayerschedule.c:10: error: syntax error before ‘char’
  6. mplayerschedule.c:12: error: syntax error before ‘char’
  7. mplayerschedule.c: In function ‘getparameters’:
  8. mplayerschedule.c:24: error: ‘new’ undeclared (first use in this function)
  9. mplayerschedule.c:24: error: syntax error before ‘char’
  10. mplayerschedule.c:28: error: syntax error before ‘char’
  11. mplayerschedule.c:31: error: syntax error before ‘char’
  12. mplayerschedule.c:35: error: syntax error before ‘char’
  13. mplayerschedule.c:38: error: ‘mplayerexec’ undeclared (first use in this function)
  14. mplayerschedule.c:38: error: syntax error before ‘char’
does "‘new’ undeclared " mean that the compiler is not recognizing "new" as a keyword? Here is my latest revised code, thanks more help you can offer on this, hopefully I'll get it next time :)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. int main()
  8. {
  9.     char* source;
  10.     source = new char [1024];
  11.     char* param;
  12.     param = new char [1024];
  13.     printf("Please Enter a Source URL to be recorded\n");
  14.     scanf("%s",&source);
  15.     printf("\nPlease Enter a file to hold the datastream from the URL\n");
  16.     scanf("%s",&param);
  17.     getparameters(source, param);
  18.     return 0;
  19. }
  20. int getparameters(char* url, char* outfile)
  21. {
  22.     //char *tempurl = url;
  23.     char* tempurl;
  24.     tempurl = new char [1024];
  25.     tempurl = url;
  26.     //char *tempoutfile = outfile;
  27.     char* tempoutfile;
  28.     tempoutfile = new char [1024];
  29.     //char *mplayer1 = "mplayer -dumpstream ";
  30.     char* mplayer1;
  31.     mplayer1 = new char [1024];
  32.     mplayer1 = "mplayer -dumpstream ";
  33.     //char *mplayer2 = " -dumpfile ";
  34.     char* mplayer2;
  35.     mplayer2 = new char [1024];
  36.     mplayer2 = " -dumpfile ";
  37.     char* mplayexec;
  38.     mplayerexec = new char [1024];
  39.     sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  40.     printf("\n");
  41.     system(mplayexec);
  42.     return 0;
  43. }
  44.  
Dec 29 '06 #5

100+
P: 145
Forgive me if I am wrong, but isn't this object oriented C++ syntax? I am using C only.
Do'h! I'm so sorry--you're completely right. I hadn't noticed that you were using C rather than C++. (A rather poor assumption on my part.)

You'll need to use malloc instead. I'll post again soon with some corrections. (I need to finish proofreading a paper before I meet with my advisor tomorrow.) Thanks, and once again, good point. -- Paul

PS: Is there a particular reason to use C instead of C++ for this project, as it appears to be a launcher for mplayer. I'm just curious. Thanks -- Paul
Dec 29 '06 #6

P: 80
Well, I really do appreciate the help you have given me thus far. I am using C for this project because I don't need to deal with too much complexity for this program and I feel that using OOP would be a little over kill. Eventually I'm planning to use it to read a text file that I have thrown links into for stream recording, but I wanted to get the most basic part of my idea up and running first, that being launching mplayer and recording a stream entered in by the user. Well, I thank you for any further help that you can provide, thank you for taking the time to help me this far.
Dec 29 '06 #7

vpawizard
P: 66
Hello,
As mentioned in previous post, you need to allocate memory for the strings source,param,etc. using malloc as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. char *source;
  3.  
  4. source = (char *) malloc(1024);
  5.  
  6.  
The prototype for malloc() is void* malloc(size_t); So the casting is required.

Regards,
Dec 29 '06 #8

P: 80
Thanks for the reply vpawizard. I tried using the "malloc()" function in the way you suggested, but I am still getting a segmentation fault error when I run the application. Should I be setting up the parameter differently here:
Expand|Select|Wrap|Line Numbers
  1. int getparameters(char *url, char *outfile)
and if I should change the parameter layout, how do I do it?

Here is the code for the entire program with my attempt at the changes you suggest. Thanks again for any help with this you can provide, hopefully I am getting closer to solution with each attempt :)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <syscall.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. int main()
  10. {
  11.     char *source;
  12.     source = (char *) malloc(1024);
  13.     char *param;
  14.     param = (char *) malloc(1024);
  15.     printf("Please Enter a Source URL to be recorded\n");
  16.     scanf("%s",&source);
  17.     printf("\nPlease Enter a file to hold the datastream from the URL\n");
  18.     scanf("%s",&param);
  19.     getparameters(source, param);
  20.     return 0;
  21. }
  22. int getparameters(char *url, char *outfile)
  23. {
  24.     //char *tempurl = url;
  25.     char *tempurl;
  26.     tempurl = (char *) malloc(1024);
  27.     tempurl = url;
  28.     //char *tempoutfile = outfile;
  29.     char *tempoutfile;
  30.     tempoutfile = (char *) malloc(1024);
  31.     //char *mplayer1 = "mplayer -dumpstream ";
  32.     char *mplayer1;
  33.     mplayer1 = (char *) malloc(1024);
  34.     mplayer1 = "mplayer -dumpstream ";
  35.     //char *mplayer2 = " -dumpfile ";
  36.     char *mplayer2;
  37.     mplayer2 = (char *) malloc(1024);
  38.     mplayer2 = " -dumpfile ";
  39.     char *mplayexec;
  40.     mplayexec = (char *) malloc(1024);
  41.     sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  42.     printf("\n %s\n",mplayexec);
  43.     printf("\n");
  44.     system(mplayexec);
  45.     return 0;
  46. }
  47.  
Dec 29 '06 #9

P: 80
I'm still pretty much lost here, if someone has any idea on what I am doing wrong as far as the allocation of memory, or the passing of the argument goes, I'd be very greatful. :)
Dec 31 '06 #10

Expert 100+
P: 1,510
for a start the variables source and param are addresses (i.e. char*) so you don't need the & (the addressof operator) in the calls to scanf, e.g. your main() then becomes
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char *source;
  4.     source = (char *) malloc(1024);
  5.     char *param;
  6.     param = (char *) malloc(1024);
  7.     printf("Please Enter a Source URL to be recorded\n");
  8.     scanf("%s",source);   // ** remove &
  9.     printf("source %s\n", source);
  10.     printf("\nPlease Enter a file to hold the datastream from the URL\n");
  11.     scanf("%s",param);      // ** remove &
  12.     printf("param %s\n", param);
  13.     getparameters(source, param);
  14.     return 0;
  15. }
  16.  
Dec 31 '06 #11

P: 80
Thank you for your reply Horace1. I have made other changes to the program because I believe that based on the point at run time when I would get a segmentation fault error before and the compiler errors that I am getting now, I believe that I have pegged my problem down as something to do with how I am passing the argument to the getparameters() function and / or how I am declaring the parameters in the getparameters() function. Here is my code as it stands now. I suppose what I reallly need help with now is getting the getparameters() set up to recieve the values correctly. So here is the program as it stands now with the changes I made:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <syscall.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. void getparameters(char url[127], char outfile[127]);
  10. int main()
  11. {
  12.         char source[127];
  13.         char param[127];
  14.         printf("Please Enter a Source URL to be recorded\n");
  15.         scanf("%s",&source);
  16.         printf("\nPlease Enter a file to hold the datastream from the URL\n");
  17.         scanf("%s",&param);
  18.         getparameters(source, param);
  19.         return 0;
  20. }
  21. void getparameters(char url[127], char outfile[127])
  22. {
  23.         char tempurl[127];
  24.         tempurl = url;
  25.         char tempoutfile[127];
  26.         tempoutfile = outfile;
  27.         char mplayer1[127];
  28.         mplayer1 = "mplayer -dumpstream ";
  29.         char mplayer2[127];
  30.         mplayer2 = " -dumpfile ";
  31.         char mplayexec[127];
  32.         sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  33.         printf("\n %s\n",mplayexec);
  34.         printf("\n");
  35.         system(mplayexec);
  36. }
If you spot problems other than the type I think I have, please feel free to explain and / or correct them.

Here are the compiler errors that I now recieve:
Expand|Select|Wrap|Line Numbers
  1. mplayerschedule.c: In function ‘getparameters’:
  2. mplayerschedule.c:24: error: incompatible types in assignment
  3. mplayerschedule.c:26: error: incompatible types in assignment
  4. mplayerschedule.c:28: error: incompatible types in assignment
  5. mplayerschedule.c:30: error: incompatible types in assignment
I get them regardless of whether or not I use scanf() like this:

Expand|Select|Wrap|Line Numbers
  1. scanf("%s",&source);
or like this:

Expand|Select|Wrap|Line Numbers
  1. scanf("%s",source);
so I don't believe I am using scanf() incorrectly. Well sorry about seemingly going about in circles here, hopefully I'm close. Thanks for any help possible.
Jan 1 '07 #12

P: 80
Horace1, I don't know what was going through my brain in my last post, I guess I woke up some what you posted clicked in my brain. Here is my corrected code that works, thanks for all of the help, but if possible, could you explain in a little bit of detail why my code wasn't working before, I only have a general idea as of now, but thanks again to all who helped me with this problem, this is a great forum! I will probably be back when I expand this into a more practical program...

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <syscall.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. int getparameters(char *url, char *outfile);
  10. int main()
  11. {
  12.     char *source;
  13.     source = (char *) malloc(1024);
  14.     char *param;
  15.     param = (char *) malloc(1024);
  16.     printf("Please Enter a Source URL to be recorded\n");
  17.     scanf("%s",source);   // ** remove &
  18.     printf("source %s\n", source);
  19.     printf("\nPlease Enter a file to hold the datastream from the URL\n");
  20.     scanf("%s",param);      // ** remove &
  21.     printf("param %s\n", param);
  22.     getparameters(source, param);
  23.     return 0;
  24. }
  25. int getparameters(char *url, char *outfile)
  26. {
  27.     //char *tempurl = url;
  28.     char *tempurl;
  29.     tempurl = (char *) malloc(1024);
  30.     tempurl = url;
  31.     //char *tempoutfile = outfile;
  32.     char *tempoutfile;
  33.     tempoutfile = (char *) malloc(1024);
  34.     tempoutfile = outfile;
  35.     //char *mplayer1 = "mplayer -dumpstream ";
  36.     char *mplayer1;
  37.     mplayer1 = (char *) malloc(1024);
  38.     mplayer1 = "mplayer -dumpstream ";
  39.     //char *mplayer2 = " -dumpfile ";
  40.     char *mplayer2;
  41.     mplayer2 = (char *) malloc(1024);
  42.     mplayer2 = " -dumpfile ";
  43.     char *mplayexec;
  44.     mplayexec = (char *) malloc(1024);
  45.     //char *mplayexec2;
  46.     //mplayexec2 = (char *) malloc(1024);
  47.     sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  48.     //sprintf(mplayexec,"%s%s",mplayexec2,tempoutfile);
  49.     printf("\n %s\n",mplayexec);
  50.     printf("\n");
  51.     system(mplayexec);
  52.     return 0;
  53. }
Jan 1 '07 #13

Expert 100+
P: 1,510

Here are the compiler errors that I now recieve:
Expand|Select|Wrap|Line Numbers
  1. mplayerschedule.c: In function ‘getparameters’:
  2. mplayerschedule.c:24: error: incompatible types in assignment
  3. mplayerschedule.c:26: error: incompatible types in assignment
  4. mplayerschedule.c:28: error: incompatible types in assignment
  5. mplayerschedule.c:30: error: incompatible types in assignment
so I don't believe I am using scanf() incorrectly. Well sorry about seemingly going about in circles here, hopefully I'm close. Thanks for any help possible.
your problem with these statements
Expand|Select|Wrap|Line Numbers
  1.         char tempurl[127];
  2.         tempurl = url;
  3.  
is that you cannot assign the contents of one array (such as url) to another (such as tempurl). With char[] arrays you can use string copy functions
Expand|Select|Wrap|Line Numbers
  1.         strncpy(tempurl, url, 127);  // ** use strncpy
  2.  
Jan 1 '07 #14

Expert 100+
P: 1,510
your new program using malloc() is a bit complex - you can use strncpy() in the previous program, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. //#include <syscall.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. void getparameters(char url[127], char outfile[127]);
  10. int main()
  11. {
  12.         char source[127];
  13.         char param[127];
  14.         printf("Please Enter a Source URL to be recorded\n");
  15.         scanf("%s", source);  // ** remove &
  16.         printf("\nPlease Enter a file to hold the datastream from the URL\n");
  17.         scanf("%s",param);  // ** remove &
  18.         getparameters(source, param);
  19.         system("pause");
  20.         return 0;
  21. }
  22. void getparameters(char url[127], char outfile[127])
  23. {
  24.         char tempurl[127];
  25.         strncpy(tempurl, url, 127);  // ** use strncpy
  26.         char tempoutfile[127];
  27.         strncpy(tempoutfile,outfile, 127); // ** use strncpy
  28.         char mplayer1[127] = "mplayer -dumpstream ";  // ** initialise
  29.         char mplayer2[127] = " -dumpfile ";
  30.         char mplayexec[127];
  31.         sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
  32.         printf("\n %s\n",mplayexec);
  33.         printf("\n");
  34.         system(mplayexec);
  35. }
Jan 1 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.