문제

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.

다른 팁

You got confused in the typedefs: You did:

typedef sample_t sub_band_t[MAXSAMPLES][MAXSAMPLES];
  • Edit:

There's an example here for a similar question: Create a pointer to two-dimensional array

So it looks like the typedef is correct, it might be the allocation of so much memory on the stack, does it still seg fault when you define MAXSAMPLES as 10? Also like he said there's the feof issue. And as I commented, your function looks to receive 6 parameters and you only send 5..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top