Thread: function fails on even # of arguments

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    14

    function fails on even # of arguments

    Hi everyone,
    What I have is a main function that takes input characters from the command prompt during the main function call, and coverts it to an integer array a using atoi. (starting at the 2nd character - the 1st is reserved for another call that I plan to reference later, and the 0th is obviously the ./function). A function is then called to find the mode of an array (the range of values in the array is 1-30). Now, when I run the whole thing, I get a segmentation fault (core dumped) for even number of arguments... Let me know if you need more explanation - It's late and I've been staring at it for too long...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int get_mode(int a[], int count);
    
    
    ////////////MAIN/////////////////
    int main(int argc, char *argv[])
    {
    
    
    int i, a[argc-2];
    //printf ("type: %s\n number of arguments = %i\n", argv[1], argc);
            
        for (i=2; i<argc; i++){
        a[i-2] = atoi(argv[i]);    
                      }
    
        for (i = 0; i<argc-2; i++){
        printf(" a[%i] = %i\n", i, a[i]);
                    }
    
    printf("Mode: %d\n", get_mode(a, argc-2));
    
    
      return 0;
    }
    
    
    
    ////////////MODE/////////////////
    
    int get_mode(int a[], int count)
    {
      int freq[31];
      int  i;
      int  mode;
     
    
    /////intializing frequency array///////
     for(i=0; i<=30; i++){
        freq[i] = 0;
                 }
    ///// counting frequencies of #'s//////////
      for (i = 0; i <= count; i++) {
        ++freq[ a[i] ];
                        }
    
    ////to see the individual frequencies/////
    //  for (i = 1; i <= 30; i++) {
    //    printf(" frequency of %i = %i\n", i, freq[i]);
    //                }
    
    
    ///finding mode//////////////
      mode = 0;
      for (i = 1; i < 30; i++) {
       if (freq[i] > mode)     {
         
        mode = i;
     }
     }
      
     
      return mode;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    count is actually 1 more than the highest index in the array, so, for this code:

    Code:
      for (i = 0; i <= count; i++) {
        ++freq[ a[i] ];
                        }
    The correct for loop header would be - for (i = 0; i < count; i++)

    This is just the output of my testing:
    Code:
    (gdb) run 2 3 5 8 8 8
    Starting program: C:\Users\Josh2\blah.exe 2 3 5 8 8 8
    [New Thread 2332.0xcb4]
     a[0] = 3
     a[1] = 5
     a[2] = 8
     a[3] = 8
     a[4] = 8
    
    Program received signal SIGSEGV, Segmentation fault.
    0x004014a6 in get_mode (a=0x28febc, count=5) at blah.c:46
    46          ++freq[ a[i] ];
    (gdb) print i
    $1 = 5
    (gdb)
    You can see that my debugger, gdb, handled the segmentation fault caused by trying to access a[5], which is memory that you can't write to.

    You may have repeated this error elsewhere, check carefully

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  2. function with variable function arguments
    By Kyl in forum C Programming
    Replies: 10
    Last Post: 12-21-2011, 02:56 PM
  3. Function arguments
    By cpudaman in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2007, 09:57 PM
  4. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  5. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 10:20 PM