0

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.

7
  • 5
    if the crash cause is from the blah function so why you do not put its code ? Commented Mar 25, 2013 at 10:38
  • Which line in blah causes the crash? What do you do there? Do you change a to point to something else? Commented Mar 25, 2013 at 10:41
  • The crash occurs before function initialization, right at the prototype line. Earlier the function was alright, but I changed it to take a two-d array as argument instead of a double pointer. I am guessing the problem is with that only, but can't pin-point. Commented Mar 25, 2013 at 10:43
  • Changing it to take an array of arrays is more correct, since you pass it an array of arrays. H Commented Mar 25, 2013 at 10:44
  • 1
    Also, blah you only send 5 parameters while looking to get 6?... Commented Mar 25, 2013 at 11:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually I was declaring more sub_band_t types in blah so shortage of stack memory would have been the problem. Thank you very much.
1

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..

1 Comment

The typedef you are suggesting is giving error: ` error: expected identifier or ‘(’ before ‘[’ token`. I picked up that typedef from here: stackoverflow.com/questions/1052818/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.