Weird bad access

I've got some really strange behaviour going on when I attempt to compile my code.

What i'm attempting to do is build a matrix recursively that represents land and sea (75% sea, 25% land), and then output the matrix to a file.


I have three functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int N=30;

int gen()
{
    return (rand()%100+1);
}

void build_grid_recursive(char board[][N],int row,int col)
{
    board[row][col]=(gen()>=25)?' ':'*';
    if(row<N) build_grid_recursive(board,(col==N)?row+1:row,(col<N)?col+1:0);
}

void output_to_file(char board[][N], ofstream& outFile)
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            outFile.put(board[0][0]);
        
        outFile.put('\n');
    }
}


If N=10,30,50,70,90 my program runs fine.
However, if N=20,40,60,80,100 (number starts with even number 2,4,6,8,10)
I get a bad access error.

Why does it do that?

Can you show us your declaration of the array?

Also, Im just wondering and I might be totally wrong.

1
2
3
4
5
6
7
for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            outFile.put(board[0][0]);
        
        outFile.put('\n');
    }


Why is there board[0][0]? Shouldnt it use the loops?
(col<N)?col+1:0); when col = N-1, you will try to access col == N, which is out of bounds.
Topic archived. No new replies allowed.