I have the following code:
#define MAXSAMPLES 1024
typedef int sample_t;
typedef sample_t sub_band_t[MAXSAMPLES][MAXSAMPLES];
void blah(sample_t a[][MAXSAMPLES], int u0, int v0, int u1, int v1) {
. . . .
}
int main(int argc, char *argv[]) {
sub_band_t in_data;
int k =0;
if (argc < 2) {
printf("\nInput filename required\n");
return 0;
}
FILE *input_file = fopen(argv[1], "r");
char del = '\0';
int i = 0, j = 0;
int cols = 0;
sample_t x;
while (! feof(input_file)) {
if (fscanf(input_file, "%d%c", &x, &del) != 2) {
i--;
break;
}
in_data[i][j] = x;
if ( del == '\n') {
i++;
j =0;
continue;
}
j++;
cols = j > cols ? j : cols;
x = 0;
}
blah(in_data, 0, 0, i, cols);
}
When I run this program with an input file with 10*10 integers, I get a segmentation fault at the blah
function call in main. I am not able to glean any information about the segmentation fault using gdb also, it just says:
0x0000000000400928 in blah (a=Cannot access memory at address 0x7ffffdbfe198) at blah.c
What am I doing wrong here? Any help would be highly appreciated.
You typedef subband_t
as a several MB large two dimensional array. That would require several MB of stack memory. Whether that works is a matter of quality of implementation. Does the program segfault for #define MAXSAMPLES 10
? Then that's your problem.
And note that
while (! feof(input_file)) { ... }
has never worked and never will because the EOF flag is only set after an input operation hit EOF. See the comp.lang.c FAQ.