Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: C - get segmentation fault right at the start of main

  1. #1
    squakie is offline Chocolate-Covered Ubuntu Beans
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    C - get segmentation fault right at the start of main

    For some reason I'm getting a segment fault for reasons I don't understand.

    I run via:
    Code:
    dave@davePC:~/juno-address-book$ ./adconvert test-addrbook.nv juno testoutput  juno-csv
    I get:
    Code:
    Segmentation fault (core dumped)
    dave@davePC:~/juno-address-book$
    From this code:
    Code:
    int main ( int argc, char *argv[] )
    {
    
    printf("\n\nbefore argc processing.....\n\n");
       if (argc != 5)
         {
            printf("\n\n************************************************\n");
            printf("You didn't specify all 4 arguement:\n");
            printf("    1st = input address book file\n");
            printf("    2nd = input address book format\n");
            printf("        juno = Juno .nv format\n");
            printf("    3nd = output file\n");
            printf("    4th = type of conversion:\n");
            printf("        gmail-csv = output csv file in gmail format\n");
            printf("        tbird-csv = output csv file in thunderbird format\n");
            printf("Can't continue, stopping......");
            printf("\n\n************************************************\n");
            return -1;
         }
    printf("/n/nafter argc processing....\n\n");
    No output from the printf's - and they are newline terminated. Shouldn't I at least get the first printf - "before argc processing....." , or am I not understanding how something works again? This DID work until I added an extra arguement (the 2nd). It worked with all 4 once. Ever since I've been getting segmentation faults right away.

    I'm trying to clean up my code by adding the command line arguement processing and changing everything over to strings (I got errors in compilation before so I was using char arrays and memcpy - then I found out I was supposed to malloc the strings before I used them )

  2. #2
    squakie is offline Chocolate-Covered Ubuntu Beans
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C - get segmentation fault right at the start of main

    Well, I just ran it with no arguements and the checking of argc worked:
    Code:
    ./abconvert
    
    
    before argc processing.....
    
    
    
    ************************************************
    You didn't specify all 4 arguement:
        1st = input address book file
        2nd = input address book format
            juno = Juno .nv format
        3nd = output file
        4th = type of conversion:
            gmail-csv = output csv file in gmail format
            tbird-csv = output csv file in thunderbird format
    Can't continue, stopping......
    
    ************************************************
    So it must be somewhere in the next bit of processing - this is the part where I tried converting to strings instead of char arrays.

    That immediately following code:
    Code:
    printf("\n\nparsing input format....\n\n");
      my_input_format = malloc(strlen(argv[3]));
      my_input_format = "\0";
    printf("\n\ngetting input type...\n\n");
      strcpy(my_input_format, argv[2]); 
      wrk1 = 0;
    printf("\n\n");
      while (wrk1 = 0)
         {
            if (0 == strcmp(my_input_format, "juno"))
                break;
            printf("\n\nInput format unknown: %s\n", my_input_format);
            printf("    Must be:\n");
            printf("        juno for input in Juno .nv format\n");
            printf("(\n");
            printf("*    Can't continue.......aborting\n\n");
         }
    printf("\n\ninput type: %s\n\n", my_input_format);
    
    
      my_output_format = malloc(strlen(argv[3]));
      strcpy(my_output_format, argv[4]); 
      if (0 == strcmp(my_output_format, "gmail-csv"))
      wrk1 = 0;
    
      while (wrk1 = 0)
         {
            if (0 ==  strcmp(my_output_format, "gmail-csv"))
                break;
            if (0 == strcmp(my_output_format, "tbird-csv"))
                break;
            printf("\n\nOutput format unknown: %s\n", my_output_format);
            printf("    Must be:\n");
            printf("        gmail-csv for gmail csv format\n");
            printf("        tbird-csv for Thunderbird csv format\n");
            printf("\n");
            printf("*    Can't continue.......aborting\n\n");
         }
    Where the 2 receiving strings are defined as:
    Code:
    char * my_input_format;
    char * my_output_format;
    I still don't get any of the printf's from that either.

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,861
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: C - get segmentation fault right at the start of main

    Your code works for me. The problem must be elsewhere.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,089
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C - get segmentation fault right at the start of main

    Quote Originally Posted by squakie View Post
    Code:
      my_input_format = malloc(strlen(argv[3]));
      my_input_format = "\0";  /* NO NO NO NO NO */
    printf("\n\ngetting input type...\n\n");
      strcpy(my_input_format, argv[2]); 
    
    
    
    
      my_output_format = malloc(strlen(argv[3]));
      strcpy(my_output_format, argv[4]);
    The idiom you are looking for is
    Code:
    my_input_format = malloc(1 + strlen(argv[3]);
    strcpy(my_input_format, argv[3]);
    or more succinctly
    Code:
    my_input_format = strdup(argv[3]);
    Well, I'm not sure whether you want 2 or 3 but you certainly want the same in both lines. Also, throwing away the pointer returned from malloc by
    Code:
      my_input_format = "\0";  /* NO NO NO NO NO */
    is just insane.


    Code:
      while (wrk1 = 0)
    This ASSIGNMENT will always evaluate to false, so neither of your loops is entered. You probably mean
    Code:
      while (wrk1 == 0)
    Last edited by spjackson; March 3rd, 2014 at 06:00 PM. Reason: Add strdup

  5. #5
    squakie is offline Chocolate-Covered Ubuntu Beans
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C - get segmentation fault right at the start of main

    Thanks! All be trying that tongiht!

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,239
    Distro
    Kubuntu

    Re: C - get segmentation fault right at the start of main

    Quote Originally Posted by squakie View Post
    Thanks! All be trying that tongiht!
    Why are you copying the input arguments given to the program? Do you plan to manipulate them somehow, or are your plans (as you have shown thus far) merely to access them for read-purposes?

    You could do something similar to the following if you merely need to read the values, although it too is unnecessary:
    Code:
    int main(int argc, char* argv[])
    {
        ...
    
        char* my_input_format = argv[3];    // define a pointer that references argv[3].
    
        ...
    }

  7. #7
    squakie is offline Chocolate-Covered Ubuntu Beans
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C - get segmentation fault right at the start of main

    I actually do read the input and output strings in other functions outside of main. I'm probably being stupid here again, but I thought you had to define globals outside of main(). So, when I define the globals can I do the same "set"? I thought those values where actually set by the compiler, the others like you show in main() at run-time. So, if I need those strings to be global, I assume there is just probably something I don't know right now - like can it say globa char* my_input_format = argv[2]; ?

    Thanks for the input - I did change the malloc's to 1+ the string length and it works. I was thinking strlen retun a relative-1 value so that if I malloc'd just the strlen it would be relative 0 so I would get the extra byte. Something else I apparently don't have right

    I'll be trying some more of the suggestions either later tonight or tomorrow. I want to get all my screwy code converted over to just using strings. When that part works - which with your help I *think* I know how to do now - then I can go back and set up the command line arguement receptor strings as you show. It would be a lot cleaner that way!

    Thanks again!

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,239
    Distro
    Kubuntu

    Re: C - get segmentation fault right at the start of main

    Quote Originally Posted by squakie View Post
    I actually do read the input and output strings in other functions outside of main. I'm probably being stupid here again, but I thought you had to define globals outside of main(). So, when I define the globals can I do the same "set"? I thought those values where actually set by the compiler, the others like you show in main() at run-time. So, if I need those strings to be global, I assume there is just probably something I don't know right now - like can it say globa char* my_input_format = argv[2]; ?
    Yes, that would indeed work. However, the question that begs to be asked is why are you declaring global variables? That's a sign of a poor design. As for the input parameters to the program, they remain there for the life of the program... but the only 'handle' you have to them is (initially) in the main function. You can of course pass these 'handles' (pointers) to other functions. For example:
    Code:
    #include <stdio.h>
    
    void printProgramName(const char* name)
    {
        printf("Your program has the following name: %s\n", name);
    }
    
    int main(int argc, char** argv)
    {
        printProgramName(argv[0]);
        return 0;
    }
    Thanks for the input - I did change the malloc's to 1+ the string length and it works. I was thinking strlen retun a relative-1 value so that if I malloc'd just the strlen it would be relative 0 so I would get the extra byte. Something else I apparently don't have right
    strlen() returns the length of the string; thus for a string like "Hello", there appears to be 5 characters... but are there? Do not forget that an extra character is there... the NULL character that is used to delimit the end of the string. Hence the reason you would need to allocate an extra byte if your intent is to make an exact copy of another string.

  9. #9
    squakie is offline Chocolate-Covered Ubuntu Beans
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C - get segmentation fault right at the start of main

    Ah, see I was thinking the strlen was relative one, while a malloc was relative 0, and hence my mistake.

    I'm just using globals throuhgout the program for 2 reasons:

    (1) I need things to be available to multiple functions, and I was lazy and didn't want to have to pass arguments in the function calls

    (2) I was lazy - I wanted easy to follow "field" names, and I wanted to keep them consistent throughout the program so it would be easier for others to know what the heck I did. I thought it would be easier for others (ok, me ) to know I was working with "input format type" than argv[2] - just easier to read is all.

    - and (C) - my C is not very good at al (as you can tell)l, so I have been having a tough time just trying to code it as is

    Thanks again for the help!

  10. #10
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,861
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: C - get segmentation fault right at the start of main

    Quote Originally Posted by squakie View Post
    Ah, see I was thinking the strlen was relative one, while a malloc was relative 0, and hence my mistake.
    "Relative" is a concept only applicable to indices. Both malloc/strlen handle sizes that are absolute. However, malloc() allocate the required size, and strlen) report the size of "meaningful" characters in a string not including the final '\0', so it reports one byte less than you need to allocate if you want to copy the string.


    Quote Originally Posted by squakie View Post
    I'm just using globals throuhgout the program for 2 reasons:

    (1) I need things to be available to multiple functions, and I was lazy and didn't want to have to pass arguments in the function calls

    (2) I was lazy - I wanted easy to follow "field" names, and I wanted to keep them consistent throughout the program so it would be easier for others to know what the heck I did. I thought it would be easier for others (ok, me ) to know I was working with "input format type" than argv[2] - just easier to read is all.

    - and (C) - my C is not very good at al (as you can tell)l, so I have been having a tough time just trying to code it as is

    Thanks again for the help!
    Take a look at the concept of struct, it allows you to keep together related values.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •