0

I was trying to make a 2D-matrix with sizes of both dimensions selected by the user (i.e. the dims[] values were assigned through scanf), and then initialize it.

My program compiles but crashes when I try to assign values to a char matrix using a function.
I guess it has something to do with the number of columns (the second brackets) and the fact that I used an array to define the size in two dimensions of the matrix. The problem only occurs when assigning values (and not, say, when printing).

int main()
{
    int dims[2] = {3,4}; 
//^The exact values are besides the point, chose some at random 
    char board[dims[0]][dims[1]];
    initialize_board(board, dims);
}

The function looks like this (MAX_SIZE is #defined as 25):

void initialize_board(char board[][MAX_SIZE], int board_side[])
{
    for(int i=0; i<board_side[0]; i++)
    {
        for(int j=0; j<board_side[1]; j++)
        {
            board[i][j]='-';
        }
    }
}

The function is declared like this:

void initialize_board(char board[][MAX_SIZE], int board_side[]);

What should I do to fix this?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dvir Moran
  • 11
  • 3
  • Where is the crash occurring? Is it on line `board[i][j]='-';`? – Treyten Carey Dec 26 '17 at 08:42
  • yes, it happens on line `board[i][j]='-';` compiler is GNU GCC..? that's what it says in Codeblocks, not sure if should say something more specific – Dvir Moran Dec 26 '17 at 08:53
  • You chose to make this question a moving target, by adding a second question (how should I design my code?) to the first one (Why does it crash as it is?). The alternative would have been to make a new separate question. Good luck with that strategy. – Yunnosch Dec 26 '17 at 09:03
  • Compile with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve your code to get no warnings. **Use the debugger `gdb`**. See also [this](https://stackoverflow.com/a/47235897/841108) – Basile Starynkevitch Dec 26 '17 at 09:07
  • `[MAX_SIZE]` in this context doesn't mean "any size" or "any size up to MAX_SIZE" or anything like that. It means "exactly MAX_SIZE". 4 is not exactly MAX_SIZE. – n. m. could be an AI Dec 26 '17 at 09:39

2 Answers2

2

Your board is of size 3*4.
Your function accesses that small board at multiples of 25 entries behind start (for j>0 in the inner loop), i.e. some 13 entry behind entry number 3*4, i.e. 14 behind the last valid entry at index 11.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Oh I see, though I'm not sure why... anyway how should I write my code differently to fix that? – Dvir Moran Dec 26 '17 at 08:45
  • For an answer to that new question (please make it one, instead of adding more questions to this one) you should provide more information on the scope and context. I.e. what are you trying to achieve. Try not to make that question an instance of the XY-problem. There is some probability of whatever you are trying to achieve requires a redesign of your API. Using a simple pointer to int and a struct of sizes as parameters is a first guess. You are experienced in the use of pointers in C, aren't you. If not https://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Yunnosch Dec 26 '17 at 08:48
  • Sorry, I'm new to this... I tried making a matrix that its values are assigned by the user (originally dims[] values were assigned through scanf) and then initialize it. I'm not at all experienced with pointers. thx anyway... – Dvir Moran Dec 26 '17 at 09:06
  • 1
    @XcoderX Asking for the full code will usually result in "I can't, because it is too long, or secret, or ...". You might want to recommend making a [mcve] instead. – Yunnosch Dec 26 '17 at 09:15
  • Okay noted @Yunnosch!! – QuIcKmAtHs Dec 26 '17 at 09:16
  • The reason I didn't post the full code is because I thought it would make it easier for the people who try to help me, no need to be a jerk. Anyway it's solved now. – Dvir Moran Dec 26 '17 at 09:48
  • Not sure who you are describing as a jerk, but making answering easier is best served by making a MCVE. If you have trouble understanding all the details of some code (which you most likely do, otherwise why ask a questions here), then it is risky to decide which parts are relevant and which are not. The (non-trivial) concept of a MCVE is the perfect synthesis of those two problems. If you can make small code which reproduces the problem, then nothing relevant is left out. Whatever you can delete is neither needed nor going to distract. And you do not let people do work which you can do. – Yunnosch Dec 26 '17 at 10:26
1

Here's how to pass a variable length array (VLA) to a function correctly.

void initialize_board(int m, int n, char board[m][n]) { ... }

int main ()
{
   int m, n;
   ... // initialize m and n here
   char board[m][n];
   initialize_board(m, n, board);
}

VLAs can be dangerous when declared as automatic variables, because they can easily crash your program when the size gets a little bit too large (a few KB or MB, depending on your OS and hardware).

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243