-
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
-
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.
-
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.
-
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.
-
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:
I don't understand why it's not assigning the new values to arr[3] and arr[4]
-
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