4

I wrote this little function in c:

void initializeArray(char arr[ROWS][COLS]){
    int i,j;
    for (i=0; i<COLS; i++){
        for (j=0; j<ROWS; j++){
            arr[i][j] = ' ';
        }
    }
}

Edit: ROWS and COLS are defined in a header file

When I call it i keep getting a segmentation fault. If I traverse the array using a pointer its okay, any ideas why?

p.s. The array being passed was defined outside a function so there's no link problems.

6
  • 1
    What is ROWS and COLS set to ? How big is your array ? Commented Aug 10, 2011 at 15:23
  • Marcelo that was actually the problem! Commented Aug 10, 2011 at 15:27
  • We need to see the call and the declaration of whatever you're passing. And note that the COLS dimension on the parameter declaration doesn't mean anything; the parameter is really a pointer, not an array. Commented Aug 10, 2011 at 15:28
  • 2
    @yotamoo: My mistake. The pointer arithmetic is different for the first and second indices, so yes, it does explain the segfault. Commented Aug 10, 2011 at 15:31
  • @Marcelo: +1 comment for acknowledging the mistake. Commented Aug 10, 2011 at 15:33

3 Answers 3

10

In your function declaration you've got ROWS as the size of the first dimension, and COLS as the size of the second dimension; but in your function body, you're looping COLS times over the first dimension, and ROWS times over the second. Depending on whether the array declaration matches the function declaration or the "implied declaration" in the code, that may be a problem.

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

1 Comment

How would the array declaration effect this?
6

In the function declaration use have char arr[ROWS][COLS] and in your loop you use arr as arr[i][j], where i is used to loop till COLS and j is used to loop till ROWS.

Your COLS and ROWS are swapped between both situations.

6 Comments

I gave you +1, but it seems you could phrase this a little better, maybe illustrate with a code example. This should be the accepted answer.
I slightly reformulated my answer, hopefully it is better phrased now. Except this being odd (hence my answer that they were swapped), I cannot come up with an idea why this would result in an segmentation fault. The fact the the OP comments that this has fixed the problem is even more peculiar...
Because if his indexes are wrong, he will access an invalid index.
Yes, after reading Marcelos answer I understood and thought that I have have known that myself on beforehand!
That's what I was trying to get you to demonstrate with a code example :)
|
5

You swapped ROWS and COLS around in the loop.

The expression arr[i][j] is equivalent to *(&arr[0][0] + COLS*i + j), and because of the swap-around, the last access becomes *(&arr[0][0] + COLS*(COLS - 1) + (ROWS - 1)).

If, e.g., COLS is 100 and ROWS is 50, then the last element accessed will be 100*99+49 = 9949 bytes from the start, but the array is only 5000 bytes. Flipping things the right way around, and the last access becomes 100*49+99 = 4999, which is the last byte of the array.

Comments

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.