5

I was trying to debug my code in another function when I stumbled upon this "weird" behaviour.

#include <stdio.h>

#define MAX 20

int main(void) {
    int matrix[MAX][MAX] = {{0}};

    return 0;
}

If I set a breakpoint on the return 0; line and I look at the local variables with Code::Blocks the matrix is not entirely filled with zeros. The first row is, but the rest of the array contains just random junk.

I know I can do a double for loop to initialize manually everything to zero, but wasn't the C standard supposed to fill this matrix to zero with the {{0}} initializer?

Maybe because it's been a long day and I'm tired, but I could've sworn I knew this.

I've tried to compile with the different standards (with the Code::Blocks bundled gcc compiler): -std=c89, -std=c99, std=c11 but it's the same.

Any ideas of what's wrong? Could you explain it to me?

EDIT: I'm specifically asking about the {{0}} initializer.

I've always thought it would fill all columns and all rows to zero.

EDIT 2: I'm bothered specifically with Code::Blocks and its bundled GCC. Other comments say the code works on different platforms. But why wouldn't it work for me? :/

Thanks.

Zorgatone
  • 4,176
  • 4
  • 30
  • 47
  • 1
    well this answer says you're doing it right http://stackoverflow.com/a/1688758/1339987 – djechlin Nov 10 '15 at 22:16
  • Using 2-dimensional arrays is something people like to do in school, but in practice it often causes more trouble than it's worth. Why not just use a single dimensional array, where the first MAX elements are assumed to be the first row, the next MAX elements are the second row, etc. dereference by matrix[row * NCOLS+col]; In that case, you can more portably initialize it as simply matrix[NCOLS * NROWS]={0}; – Dmitri Nov 10 '15 at 22:16
  • @Dmitri because you have to assume the first MAX elements are the first row, do math, the deference with more math - I believe you answered your question :P – djechlin Nov 10 '15 at 22:17
  • 1
    Bacause then I want to work on a sub-matrix in the top-left corner and it wold be a big headache to skip the outer parts – Zorgatone Nov 10 '15 at 22:18
  • The code you posted worked correctly on my MAC OS X. The matrix is all initialized to 0. – terence hill Nov 10 '15 at 22:18
  • That's why I'm bothered with `Code::Blocks` and its bundled `GCC` – Zorgatone Nov 10 '15 at 22:19
  • 10
    If you're compiling with optimizations you can't trust what the debugger tells you. And even without optimizations I wouldn't trust it 100%. Print out the values in your program to be sure. – interjay Nov 10 '15 at 22:21
  • 4
    Make sure to use the object somewhere with a side-effect to ensure the object is not actually being optimized out by the compiler. – ouah Nov 10 '15 at 22:25
  • @interjay: that worked. thanks – Zorgatone Nov 10 '15 at 22:25
  • Even if I hadn't any optimization flags on – Zorgatone Nov 10 '15 at 22:25

2 Answers2

3

I've figured it out.

Even without any optimization flag on the compiler, the debugger information was just wrong..

So I printed out the values with two for loops and it was initialized correctly, even if the debugger said otherwise (weird).

Thanks however for the comments

Zorgatone
  • 4,176
  • 4
  • 30
  • 47
-1

Your code should initialize it to zero. In fact, you can just do int matrix[MAX][MAX] = {};, and it will be initialized to 0. However, int matrix[MAX][MAX] = {{1}}; will only set matrix[0][0] to 1, and everything else to 0.

I suspect what you are observing with Code::Blocks is that the debugger (gdb?) is not quite showing you exactly where it is breaking in the code - either that or some other side-effect from the optimizer. To test that theory, add the following loop immediately after the initialization:

``` int i,j;

for (i = 0; i < MAX; i++)
  for (j = 0; j < MAX; j++)
     printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);

```

and see if what it prints is consistent with the output of the debugger.

I am going to guess that what might be happening is that since you are not using matrix the optimizer might have decided to not initialize it. To verify, disassemble your main (disass main in gdb and see if the matrix is actually being initialized.

Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20
  • 1
    `int matrix[MAX][MAX] = {};` is not allowed in Standard C – M.M Nov 11 '15 at 02:39
  • I've already found the answer. You can't just put the `{}` without a zero in it, and a double brackets is better `{{0}}`. Also the array was being initialized correctly, I figured out either the debugger was just showing the cells wrong or the compiler was doing some optimization under the hood (without me setting any optimization flags) – Zorgatone Nov 11 '15 at 07:39
  • Also I was using the matrix later in the code, I'm just not showing the rest of the program because it's irrelevant, as I'm pausing the debugger just after the declaration and initialization – Zorgatone Nov 11 '15 at 07:42