Command line args & seg faults
EDIT: thanks
ataxi for pointing out my now obvious goof up!
Hey all. This is my first post here. Be gentle ;)
I'm working on a project for school, and unfortunately I'm having problems understanding why I'm getting seg faults with my command line arguments.
Here are the important bits of my code: (The fn's Get2DSpace and Free2DSpace were written by my teacher & I know they work fine... and am assuming I'm using them correctly. Inputs: # of rows & cols to be in the array. Output: The pointer to the memory)
When I enter in 3 or 4 command line args, my program runs just fine. When I enter in only 1 or 2, I seg fault. Can anyone point out where I'm messing up?
Just to illustrate, here's typical output (from unedited code & debugging printf statements):
Any input you have is greatly appreciated!
Hey all. This is my first post here. Be gentle ;)
I'm working on a project for school, and unfortunately I'm having problems understanding why I'm getting seg faults with my command line arguments.
Here are the important bits of my code: (The fn's Get2DSpace and Free2DSpace were written by my teacher & I know they work fine... and am assuming I'm using them correctly. Inputs: # of rows & cols to be in the array. Output: The pointer to the memory)
#include/* ---------------------MAIN------------------- */ int main( int argc, char *argv[] ) { FILE *ofp; int **gameboard, **moves; int rows = 0, cols = 0; rows = atoi(argv[1]); cols = atoi(argv[2]); /*error check for args and file opening*/ if (argc != 4) { fprintf (stderr, "This program requires command line arguments\n"); fprintf (stderr, "Please enter the number of rows, number of \n"); fprintf (stderr, "columns, and the name of the output file. \n\n"); exit (-1); } ofp = fopen (argv[3], "w"); if (ofp == NULL) { fprintf(stderr, "Sorry, couldn't open output file:\n"); fprintf(stderr, "%s\n", argv[2]); exit (-2); } /*allocate space for the two arrays */ /* I left this part in because the whole memory allocation thing is where I've historically overlooked things & /* gotten seg faults */ gameboard = GetTwoDSpace(rows, cols); moves = GetTwoDSpace(rows, cols); /* edited out a bunch of stuff that is mostly printf statements and stuff that shouldn't cause seg faults */ /* close files and free up memory */ fclose(ofp); FreeTwoDSpace(gameboard, rows); FreeTwoDSpace(moves, rows); return(0); } /* ------------------------------------------ */
When I enter in 3 or 4 command line args, my program runs just fine. When I enter in only 1 or 2, I seg fault. Can anyone point out where I'm messing up?
Just to illustrate, here's typical output (from unedited code & debugging printf statements):
% a.out Segmentation Fault % a.out 3 Segmentation Fault % a.out 3 5 rows = 3 cols = 5 This program requires command line arguments Please enter the number of rows, number of columns, and the name of the output file. % a.out 3 5 more rows = 3 cols = 5 argv[0] = a.out argv[1] = 3 argv[2] = 5 argv[3] = more (reading the file more prints this same information) %
Any input you have is greatly appreciated!
