Segmentation Fault - No Idea Why Tried the Little I know

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnixuser
    New Member
    • Dec 2006
    • 80

    Segmentation Fault - No Idea Why Tried the Little I know

    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.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <syscall.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    int main()
    {
            char *source;
            char *param;
            printf("Please Enter a Source URL to be recorded\n");
            sscanf("%s",&source);
            printf("\nPlease Enter a file to hold the datastream from the URL\n");
            sscanf("%s",&param);
            getparameters(source, param);
            return 0;
    }
    int getparameters(char *url, char *outfile)
    {
            char *tempurl = url;
            char *tempoutfile = outfile;
            char *mplayer1 = "mplayer -dumpstream ";
            char *mplayer2 = " -dumpfile ";
            char *mplayexec;
            sprintf(mplayexec,"%c%c%c%c",mplayer1,tempurl,mplayer2,tempoutfile);
            printf("\n");
            system(mplayerexec);
            return 0;
    }
  • macklin01
    New Member
    • Aug 2005
    • 145

    #2
    Originally posted by cnixuser
    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.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <syscall.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    int main()
    {
            char *source;
            char *param;
            printf("Please Enter a Source URL to be recorded\n");
            sscanf("%s",&source);
            printf("\nPlease Enter a file to hold the datastream from the URL\n");
            sscanf("%s",&param);
            getparameters(source, param);
            return 0;
    }
    int getparameters(char *url, char *outfile)
    {
            char *tempurl = url;
            char *tempoutfile = outfile;
            char *mplayer1 = "mplayer -dumpstream ";
            char *mplayer2 = " -dumpfile ";
            char *mplayexec;
            sprintf(mplayexec,"%c%c%c%c",mplayer1,tempurl,mplayer2,tempoutfile);
            printf("\n");
            system(mplayerexec);
            return 0;
    }
    You need to allocate memory for the following: source, param, mplayexec. e.g.,

    Code:
    char* source;
    source = new char [1024];
    That should probably do it. -- Paul

    Comment

    • cnixuser
      New Member
      • Dec 2006
      • 80

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

      Code:
      mplayerschedule.c: In function ‘getparameters’:
      mplayerschedule.c:25: error: incompatible types in assignment
      mplayerschedule.c:32: error: incompatible types in assignment
      mplayerschedule.c:36: error: incompatible types in assignment
      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.

      Code:
      #include <stdio.h>
      #include <string.h>
      #include <syscall.h>
      #include <unistd.h>
      #include <stdio.h>
      #include <sys/types.h>
      int main()
      {
      	char* source[1024];
      	//source = new char* [1024];
      	char* param[1024];
      	//param = new char* [1024];
      	printf("Please Enter a Source URL to be recorded\n");
      	scanf("%s",&source);
      	printf("\nPlease Enter a file to hold the datastream from the URL\n");
      	scanf("%s",&param);
      	getparameters(source, param);
      	return 0;
      }
      int getparameters(char* url[1024], char* outfile[1024])
      {
      	//char *tempurl = url;
      	char* tempurl[1024];
      	//tempurl = new char* [1024];
      	tempurl = url;
      	//char *tempoutfile = outfile;
      	char* tempoutfile[1024];
      	//tempoutfile = new char* [1024];
      	//char *mplayer1 = "mplayer -dumpstream ";
      	char* mplayer1[1024];
      	//mplayer1 = new char* [1024];
      	mplayer1 = "mplayer -dumpstream ";
      	//char *mplayer2 = " -dumpfile ";
      	char* mplayer2[1024];
      	//mplayer2 = new char* [1024];
      	mplayer2 = " -dumpfile ";
      	char* mplayexec[1024];
      	//mplayerexec = new char* [1024];
      	sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
      	printf("\n");
      	system(mplayexec);
      	return 0;
      }

      Comment

      • macklin01
        New Member
        • Aug 2005
        • 145

        #4
        Originally posted by cnixuser
        Well, I tried allocating memory for each of the variables as Paul suggested, but now I get the following compiler errors *snip*
        Code:
        	char* source[1024];
        	//source = new char* [1024];
        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):

        Code:
        char* source;
        source = new char [1024];
        char* param;
        param =new char [1024];
        and so forth.

        Also, this is perfectly fine:
        Code:
        char* blah = "blah blah blah blah";
        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

        Comment

        • cnixuser
          New Member
          • Dec 2006
          • 80

          #5
          Forgive me if I am wrong, but isn't this object oriented C++ syntax? I am using C only.
          Code:
          char* tempoutfile;
          	tempoutfile = new char [1024];
          When I do as you suggest I get the following compiler errors:
          Code:
          mplayerschedule.c: In function ‘main’:
          mplayerschedule.c:10: error: ‘new’ undeclared (first use in this function)
          mplayerschedule.c:10: error: (Each undeclared identifier is reported only once
          mplayerschedule.c:10: error: for each function it appears in.)
          mplayerschedule.c:10: error: syntax error before ‘char’
          mplayerschedule.c:12: error: syntax error before ‘char’
          mplayerschedule.c: In function ‘getparameters’:
          mplayerschedule.c:24: error: ‘new’ undeclared (first use in this function)
          mplayerschedule.c:24: error: syntax error before ‘char’
          mplayerschedule.c:28: error: syntax error before ‘char’
          mplayerschedule.c:31: error: syntax error before ‘char’
          mplayerschedule.c:35: error: syntax error before ‘char’
          mplayerschedule.c:38: error: ‘mplayerexec’ undeclared (first use in this function)
          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 :)

          Code:
          #include <stdio.h>
          #include <string.h>
          #include <syscall.h>
          #include <unistd.h>
          #include <stdio.h>
          #include <sys/types.h>
          int main()
          {
          	char* source;
          	source = new char [1024];
          	char* param;
          	param = new char [1024];
          	printf("Please Enter a Source URL to be recorded\n");
          	scanf("%s",&source);
          	printf("\nPlease Enter a file to hold the datastream from the URL\n");
          	scanf("%s",&param);
          	getparameters(source, param);
          	return 0;
          }
          int getparameters(char* url, char* outfile)
          {
          	//char *tempurl = url;
          	char* tempurl;
          	tempurl = new char [1024];
          	tempurl = url;
          	//char *tempoutfile = outfile;
          	char* tempoutfile;
          	tempoutfile = new char [1024];
          	//char *mplayer1 = "mplayer -dumpstream ";
          	char* mplayer1;
          	mplayer1 = new char [1024];
          	mplayer1 = "mplayer -dumpstream ";
          	//char *mplayer2 = " -dumpfile ";
          	char* mplayer2;
          	mplayer2 = new char [1024];
          	mplayer2 = " -dumpfile ";
          	char* mplayexec;
          	mplayerexec = new char [1024];
          	sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
          	printf("\n");
          	system(mplayexec);
          	return 0;
          }
          Last edited by cnixuser; Dec 29 '06, 06:21 AM. Reason: forgot to add some code

          Comment

          • macklin01
            New Member
            • Aug 2005
            • 145

            #6
            Originally posted by cnixuser
            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

            Comment

            • cnixuser
              New Member
              • Dec 2006
              • 80

              #7
              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.

              Comment

              • vpawizard
                New Member
                • Nov 2006
                • 66

                #8
                Hello,
                As mentioned in previous post, you need to allocate memory for the strings source,param,et c. using malloc as follows:

                Code:
                char *source;
                
                source = (char *) malloc(1024);
                The prototype for malloc() is void* malloc(size_t); So the casting is required.

                Regards,

                Comment

                • cnixuser
                  New Member
                  • Dec 2006
                  • 80

                  #9
                  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:
                  Code:
                  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 :)

                  Code:
                  #include <stdio.h>
                  #include <malloc.h>
                  #include <stdlib.h>
                  #include <string.h>
                  #include <syscall.h>
                  #include <unistd.h>
                  #include <stdio.h>
                  #include <sys/types.h>
                  int main()
                  {
                  	char *source;
                  	source = (char *) malloc(1024);
                  	char *param;
                  	param = (char *) malloc(1024);
                  	printf("Please Enter a Source URL to be recorded\n");
                  	scanf("%s",&source);
                  	printf("\nPlease Enter a file to hold the datastream from the URL\n");
                  	scanf("%s",&param);
                  	getparameters(source, param);
                  	return 0;
                  }
                  int getparameters(char *url, char *outfile)
                  {
                  	//char *tempurl = url;
                  	char *tempurl;
                  	tempurl = (char *) malloc(1024);
                  	tempurl = url;
                  	//char *tempoutfile = outfile;
                  	char *tempoutfile;
                  	tempoutfile = (char *) malloc(1024);
                  	//char *mplayer1 = "mplayer -dumpstream ";
                  	char *mplayer1;
                  	mplayer1 = (char *) malloc(1024);
                  	mplayer1 = "mplayer -dumpstream ";
                  	//char *mplayer2 = " -dumpfile ";
                  	char *mplayer2;
                  	mplayer2 = (char *) malloc(1024);
                  	mplayer2 = " -dumpfile ";
                  	char *mplayexec;
                  	mplayexec = (char *) malloc(1024);
                  	sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
                  	printf("\n %s\n",mplayexec);
                  	printf("\n");
                  	system(mplayexec);
                  	return 0;
                  }

                  Comment

                  • cnixuser
                    New Member
                    • Dec 2006
                    • 80

                    #10
                    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. :)

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      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
                      Code:
                      int main()
                      {
                      	char *source;
                      	source = (char *) malloc(1024);
                      	char *param;
                      	param = (char *) malloc(1024);
                      	printf("Please Enter a Source URL to be recorded\n");
                      	scanf("%s",source);   // ** remove &
                      	printf("source %s\n", source);
                      	printf("\nPlease Enter a file to hold the datastream from the URL\n");
                      	scanf("%s",param);      // ** remove &
                      	printf("param %s\n", param);
                      	getparameters(source, param);
                      	return 0;
                      }

                      Comment

                      • cnixuser
                        New Member
                        • Dec 2006
                        • 80

                        #12
                        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:

                        Code:
                        #include <stdio.h>
                        #include <malloc.h>
                        #include <stdlib.h>
                        #include <string.h>
                        #include <syscall.h>
                        #include <unistd.h>
                        #include <stdio.h>
                        #include <sys/types.h>
                        void getparameters(char url[127], char outfile[127]);
                        int main()
                        {
                                char source[127];
                                char param[127];
                                printf("Please Enter a Source URL to be recorded\n");
                                scanf("%s",&source);
                                printf("\nPlease Enter a file to hold the datastream from the URL\n");
                                scanf("%s",&param);
                                getparameters(source, param);
                                return 0;
                        }
                        void getparameters(char url[127], char outfile[127])
                        {
                                char tempurl[127];
                                tempurl = url;
                                char tempoutfile[127];
                                tempoutfile = outfile;
                                char mplayer1[127];
                                mplayer1 = "mplayer -dumpstream ";
                                char mplayer2[127];
                                mplayer2 = " -dumpfile ";
                                char mplayexec[127];
                                sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
                                printf("\n %s\n",mplayexec);
                                printf("\n");
                                system(mplayexec);
                        }
                        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:
                        Code:
                        mplayerschedule.c: In function ‘getparameters’:
                        mplayerschedule.c:24: error: incompatible types in assignment
                        mplayerschedule.c:26: error: incompatible types in assignment
                        mplayerschedule.c:28: error: incompatible types in assignment
                        mplayerschedule.c:30: error: incompatible types in assignment
                        I get them regardless of whether or not I use scanf() like this:

                        Code:
                        scanf("%s",&source);
                        or like this:

                        Code:
                        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.
                        Last edited by cnixuser; Jan 1 '07, 03:08 AM. Reason: I forgot to add the compiler errors

                        Comment

                        • cnixuser
                          New Member
                          • Dec 2006
                          • 80

                          #13
                          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...

                          Code:
                          #include <stdio.h>
                          #include <malloc.h>
                          #include <stdlib.h>
                          #include <string.h>
                          #include <syscall.h>
                          #include <unistd.h>
                          #include <stdio.h>
                          #include <sys/types.h>
                          int getparameters(char *url, char *outfile);
                          int main()
                          {
                          	char *source;
                          	source = (char *) malloc(1024);
                          	char *param;
                          	param = (char *) malloc(1024);
                          	printf("Please Enter a Source URL to be recorded\n");
                          	scanf("%s",source);   // ** remove &
                          	printf("source %s\n", source);
                          	printf("\nPlease Enter a file to hold the datastream from the URL\n");
                          	scanf("%s",param);      // ** remove &
                          	printf("param %s\n", param);
                          	getparameters(source, param);
                          	return 0;
                          }
                          int getparameters(char *url, char *outfile)
                          {
                          	//char *tempurl = url;
                          	char *tempurl;
                          	tempurl = (char *) malloc(1024);
                          	tempurl = url;
                          	//char *tempoutfile = outfile;
                          	char *tempoutfile;
                          	tempoutfile = (char *) malloc(1024);
                          	tempoutfile = outfile;
                          	//char *mplayer1 = "mplayer -dumpstream ";
                          	char *mplayer1;
                          	mplayer1 = (char *) malloc(1024);
                          	mplayer1 = "mplayer -dumpstream ";
                          	//char *mplayer2 = " -dumpfile ";
                          	char *mplayer2;
                          	mplayer2 = (char *) malloc(1024);
                          	mplayer2 = " -dumpfile ";
                          	char *mplayexec;
                          	mplayexec = (char *) malloc(1024);
                          	//char *mplayexec2;
                          	//mplayexec2 = (char *) malloc(1024);
                          	sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
                          	//sprintf(mplayexec,"%s%s",mplayexec2,tempoutfile);
                          	printf("\n %s\n",mplayexec);
                          	printf("\n");
                          	system(mplayexec);
                          	return 0;
                          }

                          Comment

                          • horace1
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 1510

                            #14
                            Originally posted by cnixuser

                            Here are the compiler errors that I now recieve:
                            Code:
                            mplayerschedule.c: In function ‘getparameters’:
                            mplayerschedule.c:24: error: incompatible types in assignment
                            mplayerschedule.c:26: error: incompatible types in assignment
                            mplayerschedule.c:28: error: incompatible types in assignment
                            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
                            Code:
                                    char tempurl[127];
                                    tempurl = url;
                            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
                            Code:
                                    strncpy(tempurl, url, 127);  // ** use strncpy

                            Comment

                            • horace1
                              Recognized Expert Top Contributor
                              • Nov 2006
                              • 1510

                              #15
                              your new program using malloc() is a bit complex - you can use strncpy() in the previous program, e.g.
                              Code:
                              #include <stdio.h>
                              #include <malloc.h>
                              #include <stdlib.h>
                              #include <string.h>
                              //#include <syscall.h>
                              #include <unistd.h>
                              #include <stdio.h>
                              #include <sys/types.h>
                              void getparameters(char url[127], char outfile[127]);
                              int main()
                              {
                                      char source[127];
                                      char param[127];
                                      printf("Please Enter a Source URL to be recorded\n");
                                      scanf("%s", source);  // ** remove &
                                      printf("\nPlease Enter a file to hold the datastream from the URL\n");
                                      scanf("%s",param);  // ** remove &
                                      getparameters(source, param);
                                      system("pause");
                                      return 0;
                              }
                              void getparameters(char url[127], char outfile[127])
                              {
                                      char tempurl[127];
                                      strncpy(tempurl, url, 127);  // ** use strncpy
                                      char tempoutfile[127];
                                      strncpy(tempoutfile,outfile, 127); // ** use strncpy
                                      char mplayer1[127] = "mplayer -dumpstream ";  // ** initialise
                                      char mplayer2[127] = " -dumpfile ";
                                      char mplayexec[127];
                                      sprintf(mplayexec,"%s%s%s%s",mplayer1,tempurl,mplayer2,tempoutfile);
                                      printf("\n %s\n",mplayexec);
                                      printf("\n");
                                      system(mplayexec);
                              }

                              Comment

                              Working...