Thread: I get this error Segmentation fault in my program when I run it, and I don't know why

  1. #16
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    This is the whole thing
    I attached the driver also main program that I was trying to create the algorithm.

    Code:
     / Driver
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <math.h>
    #include <getopt.h>
    #include <sys/time.h>
    
    /* getopt.h */
    extern char *optarg;
    
    /* stdlib.h */
    extern void srand48(long seed);
    extern double drand48(void);
    
    /* this function is implemented in cntSort.c */
    extern void cntSort(unsigned m, unsigned n, unsigned data[]);
    
    /* comparison function for qsort() */
    static int cmpUnsigned(const void *a, const void *b)
    {
    return *(unsigned *)a - *(unsigned *)b;
    }
    
    int main(int argc, char* argv[])
    {
    unsigned m, n, *data, *qsorted, i;
    struct timeval start, stop;
    float msec;
    long seed;
    int c;
    
    /* command line options
    * ./cntSort -m mValue -n nValue -s seedValue
    * m default 64
    * n default 2^20, 1M
    * seed default 2018
    */
    m = 64;
    n = 1 << 20;
    seed = 2018;
    /* process optional command line arguments */
    while ((c = getopt(argc, argv, "m:n:s:")) != -1) {
    switch (c) {
    case 'm': sscanf(optarg, "%u", &m); break;
    case 'n': sscanf(optarg, "%u", &n); break;
    case 's': sscanf(optarg, "%ld", &seed); break;
    default: break;
    }
    }
    printf("m %u, n %u, seed %ld\n", m, n, seed);
    
    /* seeding the pseudorandom number generator */
    srand48(seed);
    
    /* allocate memory -- remember to free up them later */
    data = (unsigned *)malloc(sizeof(unsigned) * n);
    qsorted = (unsigned *)malloc(sizeof(unsigned) * n);
    
    /* initialize data */
    for (i = 0; i < n; i++)
    qsorted[i] = data[i] = (unsigned) floor(drand48() * m);
    
    /* time qsort */
    gettimeofday(&start, NULL);
    qsort((void *)qsorted, n, sizeof(unsigned), cmpUnsigned);
    gettimeofday(&stop, NULL);
    msec = (stop.tv_sec - start.tv_sec) * 1000 +
    (stop.tv_usec - start.tv_usec) / 1000;
    printf("quicksort takes %.2f msec\n", msec);
    
    /* time cntSort */
    gettimeofday(&start, NULL);
    cntSort(m, n, data);
    gettimeofday(&stop, NULL);
    msec = (stop.tv_sec - start.tv_sec) * 1000 +
    (stop.tv_usec - start.tv_usec) / 1000;
    printf("cntSort takes %.2f msec\n", msec);
    
    /* verify cntSort works correctly */
    for (i = 0; i < n; i++)
    if (data[i] != qsorted[i]) {
    fprintf(stderr, "data differs from qsorted at the %u-th element\n", i);
    exit(1);
    }
    /* free up memory */
    free(qsorted);
    free(data);
    
    return 0;
    }
    Code:
     / Main program to create the algorithm
    
    #include <stdlib.h>
    
    void cntSort(unsigned m, unsigned n, unsigned data[])
    {
    unsigned *count;
    
    /* allocate memory */
    count = (unsigned *)malloc(sizeof(unsigned) * m);
    
    /* free up memory */
    free(count);
    }

  2. #17
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    Quote Originally Posted by jimblumberg View Post
    Let's start by having you post the entire program, not just a few bits and pieces.

    Next do you know how to print that data[] array? If so show us a program that prints that array.
    I attached the whole program

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,451
    Okay so now since cntSort() is really not doing anything are you still getting a segmentation fault?

    If so where does your debugger tell you the problem shows itself?

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Code:
    /* getopt.h */
    extern char *optarg;
     
    /* stdlib.h */
    extern void srand48(long seed);
    extern double drand48(void);
    Having to do the above is not normal; why are you doing it?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #20
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    The driver code is given from the prof, I cannot touch anything there the problem I'm having is with main.c

  6. #21
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    ./cntSort -m 64 -n 1048676 -s2018

    Without anything on it doesn't show any problems, I just don't know what's going on with the algorithm I created above and have no time I have to turn it in tomorrow

  7. #22
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Quote Originally Posted by erald23 View Post
    The driver code is given from the prof, I cannot touch anything there the problem I'm having is with main.c
    Are you using the exact same operating system and compiler as the Professor is doing? If not, then you should not expect this bad code to work.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by erald23 View Post
    ./cntSort -m 64 -n 1048676 -s2018

    Without anything on it doesn't show any problems, I just don't know what's going on with the algorithm I created above and have no time I have to turn it in tomorrow
    You completely deleted all the code, so saying you "created an algorithm" is ... not true. You posted a text description of the algorithm in that one post of yours; you just need to implement it.

    If you are still referring to your original code way back in post one, the algorithm you created there is:
    1. Use un-initialized data for the count (rather than setting the initial counts to zero)
    2. Process a complete set of garbage data (temp) that was grabbed for no reason whatsoever
    3. Process the actual data (but on top of the nonsense counts that were already there)
    4. Erase the good data by copying the garbage data (temp) on top of it, completely ignoring the count information (that was completely wrong anyway)

    Hopefully you can see why that algorithm doesn't actually solve your problem.

  9. #24
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    Quote Originally Posted by tabstop View Post
    You completely deleted all the code, so saying you "created an algorithm" is ... not true. You posted a text description of the algorithm in that one post of yours; you just need to implement it.

    If you are still referring to your original code way back in post one, the algorithm you created there is:
    1. Use un-initialized data for the count (rather than setting the initial counts to zero)
    2. Process a complete set of garbage data (temp) that was grabbed for no reason whatsoever
    3. Process the actual data (but on top of the nonsense counts that were already there)
    4. Erase the good data by copying the garbage data (temp) on top of it, completely ignoring the count information (that was completely wrong anyway)

    Hopefully you can see why that algorithm doesn't actually solve your problem.
    First of all I said before I'm new at C programming and that's why I'm asking help , but if you not willing to help or give me at least recommendation, dont do it at all.
    I understand you guys are experts but I'm sure you started like me at some point.
    Thanks

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by erald23 View Post
    First of all I said before I'm new at C programming and that's why I'm asking help , but if you not willing to help or give me at least recommendation, dont do it at all.
    I understand you guys are experts but I'm sure you started like me at some point.
    Thanks
    There's nothing wrong with being new; there's all sorts of things wrong with not even trying to learn -- and by the evidence you're showing here, this is where you are. Your first code was an attempt to solve the problem with lots of problems with it, that have been pointed out. Your response was to erase everything and wait for us to give you the answer. You have a list of all the things that were wrong with your original code; you have a list of the things you need to do (which we didn't even give you, that you already had yourself). If there's something specific about the list that you want to ask about, then do so, but you need to sit down and do those things.

    Just to take a particular example, the thing that's been brought up the most is "setting all the initial counts to zero". The only thing I can see that might be considered an attempt to do so in your own code is
    Code:
    count[i];
    from your second code posting. If that was really an attempt to set everything to zero, then you are a very long way from where you need to be and you need to review a lot of material. If that wasn't an attempt at this, then (a) why is it there and (b) why haven't you tried to set all your initial counts to zero?

    (For reference so you can see the difference, setting everything equal to zero looks like
    Code:
    for (int i = 0; i < whichever_of_n_or_m_you_are_using_for_this_which_is_why_you_use_real_variable_names; i++) {
        count[i] = 0;
    }
    We try to just not hand out homework answers here, but the point I'm trying to make is that each of these steps is pretty darn simple.)

  11. #26
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    Quote Originally Posted by tabstop View Post
    There's nothing wrong with being new; there's all sorts of things wrong with not even trying to learn -- and by the evidence you're showing here, this is where you are. Your first code was an attempt to solve the problem with lots of problems with it, that have been pointed out. Your response was to erase everything and wait for us to give you the answer. You have a list of all the things that were wrong with your original code; you have a list of the things you need to do (which we didn't even give you, that you already had yourself). If there's something specific about the list that you want to ask about, then do so, but you need to sit down and do those things.

    Just to take a particular example, the thing that's been brought up the most is "setting all the initial counts to zero". The only thing I can see that might be considered an attempt to do so in your own code is
    Code:
    count[i];
    from your second code posting. If that was really an attempt to set everything to zero, then you are a very long way from where you need to be and you need to review a lot of material. If that wasn't an attempt at this, then (a) why is it there and (b) why haven't you tried to set all your initial counts to zero?

    (For reference so you can see the difference, setting everything equal to zero looks like
    Code:
    for (int i = 0; i < whichever_of_n_or_m_you_are_using_for_this_which_is_why_you_use_real_variable_names; i++) {
        count[i] = 0;
    }
    We try to just not hand out homework answers here, but the point I'm trying to make is that each of these steps is pretty darn simple.)
    Thanks very much for trying to help even myself don't want you to put the answer because I want to learn. I just want to be directed in the right way since the professor doesn't offer that much help. anyways thanks again

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One thing that can help us help you out is putting in comments in the code you've written as to what you are trying to do with each chunk of code. For that matter, in this case since you have a description of what's going on you can even start with the comments, and then write the code to match.
    Code:
    //Create space to hold the counts, and initialize to zero
    count = malloc( ... );
    for (int i = 0 ...) {
        count[i] = 0;
    }
    
    //Go through data array, and count as we go
    
    //Use counts to write out data in sorted order
    
    //Clean up
    Your original code did have the proper walk-through-the-data-and-add-to-count loop, so that chunk is definitely salvageable.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by stahta01 View Post
    Code:
    /* getopt.h */
    extern char *optarg;
     
    /* stdlib.h */
    extern void srand48(long seed);
    extern double drand48(void);
    Having to do the above is not normal; why are you doing it?

    Tim S.
    I don't know if my system is unusual (latest Mint), but for me it is very difficult to get glibc to acknowledge the existence of these functions; all of the feature test macros in the manual still leave me with "implicit declaration" warnings without those lines (and in fact it thinks I want to be using srand instead). Digging out the actual stdlib.h, I was able to discover __USE_XOPEN which gets me there (__USE_MISC apparently would also work). I don't think I'm supposed to be using those directly, but neither of the "usual" user macros of _XOPEN_SOURCE or _GNU_SOURCE (which is supposed to open everything) enabled those prototypes.

    EDIT: I am an idiot who put the defines on line 2 instead of line 1, so the first header set all the flags before the define was found. Moving them to line 1 made things work as expected. The flip side of this is given <unistd.h> and <sys/time.h> assuming a unix-like system is a pretty safe bet, so including those prototypes to not mess with flags is not completely crazy.
    Last edited by tabstop; 10-24-2018 at 10:48 PM. Reason: boundless idiocy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault error [help]
    By blaze505 in forum C Programming
    Replies: 9
    Last Post: 10-21-2011, 11:38 PM
  2. GCC Segmentation fault error.
    By JPinUSMC in forum C Programming
    Replies: 2
    Last Post: 02-21-2011, 10:59 PM
  3. Replies: 24
    Last Post: 02-09-2011, 09:19 AM
  4. Segmentation fault Error
    By unknown_ in forum C Programming
    Replies: 7
    Last Post: 03-21-2010, 01:32 PM
  5. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 02:16 PM

Tags for this Thread