Segmentation Fault??

This is a discussion on Segmentation Fault?? within the C Programming forums, part of the General Programming Boards category; Hey everyone, I'm a student in a COP class and I'm writing a program (it's for a grade so please ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    Segmentation Fault??

    Hey everyone,

    I'm a student in a COP class and I'm writing a program (it's for a grade so please if you use code to explain it...make it purely hypothetical). My program compiles, but has a Segmentation Fault--I really don't know what it means.

    I'm not finished...I was just testing midway...Here's the Source Code
    Code:
    ================================================================
    /***************************************************************************
       Description: Project 4
       Due Date/Time: Friday, April 7, 2006 at 8:00 a.m.
    
       This program is designed to take and integer seed from the OS and
       dimensions for a two dimensional array and randomly fill the array of 
       specified size, calculate the mean and median of each row of data, and 
       print all data to an output file.
    ***************************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #define TRUE 1
    #define FALSE 0
    #define MAXROWS 10
    #define MAXCOLS 15
    
    
    int openFiles(int argc, char *argv[], FILE **fpIn, FILE **fpOut);
    int readData(FILE *fpIn, int *pRows, int *pCols);
    void fillArray(int seed, int rows, int cols, int arr[][]);
    void calcMeans(int rows, int cols, int arr[][], double means[]);
    void sortArr(int rows, int cols, int arr[][]);
    void sort(int cols, int arr[]);
    void swap(int *pNum1, int *pNum2);
    void display(FILE *fpOut, int rows, int cols, int arr[][], double means[]);
    void displayHeader(FILE *fpOut, int cols);
    
    
    int main(int argc, char *argv[])
    {
       FILE *fpIn, *fpOut;
       int seed, rows, cols;
       int arr[rows][cols];
    int i, j;
    
       if (openFiles(argc, argv, &fpIn, &fpOut))
       {
          seed = readData(fpIn, &rows, &cols);
          fillArray(seed, rows, cols, arr);
    
    // TEST: REMOVE!
    for (i = 0; i < rows; i++)
    {
       for (j = 0; j < cols; j++) 
       printf("%4d", arr[i][j]); 
       printf("\n");
    }
    printf("\n\n"); 
    
       }
    
       return 0;
    }
    
    
    int openFiles(int argc, char *argv[], FILE **fpIn, FILE **fpOut)
    {
       int success = FALSE;
    
       if (argc == 3)
       {
          *fpIn = fopen(argv[1], "r");
          if (*fpIn != NULL)
          {
             *fpOut = fopen(argv[2], "w");
             if (*fpOut != NULL)
                success = TRUE;
             else
             {
                printf("\n\nError: Unable to open Output File.\n\n");
                fclose(*fpOut);
             }
          }
          else
          {
             printf("\n\nError: Unable to open Input File.\n\n");
             fclose(*fpIn);
          }
       }
    
       return success;
    }
    
    
    int readData(FILE *fpIn, int *pRows, int *pCols)
    {
       int seed;
    
       fscanf(fpIn, "%d %d %d", &seed, &*pRows, &*pCols);
    
       return seed;
    }
    
    
    void fillArray(int seed, int rows, int cols, int arr[rows][cols])
    {
       int i, j;
    
       srand(seed);
       for (i = 0; i < rows; i++)
          for (j = 0; j < cols; j++)
             arr[i][j] = rand() % 71 + 30;
    }
    
    
    void calcMeans(int rows, int cols, int arr[][], double means[])
    {
    }
    
    
    void sortArr(int rows, int cols, int arr[][])
    {
    }
    
    
    void sort(int cols, int arr[])
    {
    }
    
    
    void swap(int *pNum1, int *pNum2)
    {
    }
    
    
    void display(FILE *fpOut, int rows, int cols, int arr[][], double means[])
    {
    }
    
    
    void displayHeader(FILE *fpOut, int cols)
    {
    }
    ===================================================================
    I was searchin the internet to find a way to solve it and I found a debugging compile option...but I dont know anything about it...I just used it and it follows:

    ------------
    osprey:~/cproj/proj4$ gcc -g -o prog proj4.c
    osprey:~/cproj/proj4$ gdb prog
    GNU gdb 5.0
    Copyright 2000 Free Software Foundation, Inc.

    (gdb) run input.txt output.txt
    Starting program: ~/cproj/proj4/prog input.txt output.txt

    Program received signal SIGSEGV, Segmentation fault.
    0x80485c2 in main (argc=3, argv=0xbffffcb4) at proj4.c:38
    38 if (openFiles(argc, argv, &fpIn, &fpOut))
    -----------

    So something (probably really stupid that I just missed conceptually) is wrong with my condition, yeah.

    I'd really appreciate an explanation,
    Thank you,
    Steve

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A segmentation fault means you're accessing a memory segment you shouldn't be. However:
    Code:
    int seed, rows, cols;
       int arr[rows][cols];
    Both 'rows' and 'cols' hold some arbitrary number, since you didn't initialize them. The array is created (or it tries anyway) with whatever values happen to be in those variables. Then later, I assume you enter new 'rows' and 'cols', and try to use them as the new bounds for your array. You either end up over shooting your array's real boundaries, or something like that. Or, their initial values are some huge value, which is too big for you to actually allocate locally.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, a segmentation fault occurs when your program tries to read memory not allocated to it. Now with that knowledge, go through your code and look for places it accesses memory. Make sure you're not trying to read something that's not yours.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    ohhh...ohk...well since when I declared the array, I declared it with garbage...which isnt it a reallly huge long number...it's trying to make and array in a location that either doesnt exist on the server or is something else?...

    [changing my junk]

    alright...I initialized the row and column size of my array to the #define'd max values...then it input's numbers...they're crazy...but they're numbers.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Results
    [outcome]
    osprey:~/cproj/proj4$ a.out input.txt output.txt
    33 40 31 31 32 48 87 74 33
    57 89 33 100 81 72 48 63 32
    72 98 71 34 59 73 52 45 78
    10737479861073823980107377270310739195391073919539 0107377202510738239808347
    10739196651073919665 010737720251073823980 11073826608 8361073898792
    [/outcome]

    my input file is as follows:
    Code:
    10
    5   9
    I don't understand why it's not assigning the new values to arr[3] and arr[4]

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    Ohk...I've got it...thank yall.

    Code:
    osprey:~/cproj/proj4$ a.out input.txt output.txt
      33  40  31  31  32  48  87  74  33
      51  99  62  98 100  30  57  89  33
     100  81  72  48  63  32  73  35  54
      77  95 100  72  98  71  34  59  73
      52  45  78  55  67  76  87  95  35

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21