0

I have problem with filling matrix with size bigger 721x721. I have tried to compile this code on online c compilers like https://www.onlinegdb.com/online_c_compiler. On this site my code ran without any errorsenter image description here

#include <stdio.h>
#include <stdlib.h>

int main(int args, char const *argv[])
{
    int n = 722;
    int matrix[n][n];
    
    int i, j;
    for(i = 0; i<n; i++)
        for(j = 0; j<n; j++)
            matrix[i][j] = 0;
            
    return 0;
}
Jeka X
  • 19
  • 3
  • 3
    You have a stack overflow. The stack size is limited. On windows the default stack is 1MB. On other operating systems the default is a few times that but still likely smaller than 10 MB – drescherjm Apr 18 '22 at 13:33
  • @drescherjm The code is using 722*722*4 + 722*(sizeof_a_pointer) + 3*4 bytes, ~2MB. – Zakk Apr 18 '22 at 13:40
  • 1
    @Zakk: I agree with the `722*722*4` (which is used by the array `matrix`), and I also agree with the `3*4 bytes` (which is used by the variables `n`, `i` and `j`), but I don't understand where `722*(sizeof_a_pointer)` is coming from? – Andreas Wenzel Apr 18 '22 at 13:56
  • @AndreasWenzel Every `matrix[i]` is a pointer. Therefore, you have `722 * sizeof_a_pointer`. – Zakk Apr 18 '22 at 14:04
  • 1
    @Zakk: When you write `matrix[i]`, it will [decay to a pointer](https://stackoverflow.com/q/1461432/12149471) to the first element of the sub-array in most (but not all) situations. However, that does not mean that `matrix[i]` is a pointer. I also do not believe that the compiler will store such a pointer to the start of every sub-array of `matrix` for optimization purposes, as such a pointer can be trivially calculated on demand with a simple multiplication operation. – Andreas Wenzel Apr 18 '22 at 14:17

0 Answers0