0

I'd like to know where is the mistake in my code. I just started learning C and I just got to pointers and arrays so I have no idea where the mistake is. The problem seems to be in the part where I use struct to create my own data type because none of the debug texts that I've put in show themselves in console when I run the program. I've looked on the internet for an answer but haven t found anything. Any help would be appreciated.

#include <stdio.h>
#include <stdlib.h>
#define MAX_size 2000
typedef struct TMatrix{
    int grid[MAX_size][MAX_size];
    int sizex;
    int sizey;
} TMATRIX;
int readSize(TMATRIX *matrix);

int main(void){
    TMATRIX matrix;
    printf("DEBUG\n");
    if (readSize(&matrix)==1){
        printf ("Invalid input.\n");
        return 1;
    }

    printf("%d %d\n", matrix.sizex,matrix.sizey);

    return 0;
}

int readSize(TMATRIX *matrix){
    printf("DEBUG\n");
    if (scanf("%d %d", &matrix->sizex, &matrix->sizey)!=2) return 1;
    if (matrix->sizex<1 || matrix->sizey<1) return 1;

    return 0;
}
michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
Petr Kučera
  • 78
  • 11

1 Answers1

1

In your TMATRIX structure, you declare an array of integers of 2000x2000.

2000*2000*4 is 16 megabytes. You're using an auto variable approx. that size (that if sizeof(int)==4 but that could be the double if sizeof(int)==8).

So you probably get a stack overflow before you even reach the first instruction, when the compiler-generated code tries to allocate enough stack room for your variable, typical stack sizes are smaller than that.

Fixes:

  • increase the stack (linker option)
  • use a global variable (move matrix outside the main procedure)
  • allocate your table using malloc, and not statically (to keep multidimensional aspect, define int (*grid)[MAX_size]; and make a malloc on MAX_size * sizeof *grid)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • If you place return after " TMATRIX matrix;" than the program finishes succesfuly. I can not see any auto variable anywhere. – Tomáš Šíma Nov 27 '16 at 21:45
  • 1
    @Tomᚊíma you're seeing the effect of an optimization: since the variable is unused, the compiler removed it altogether. – Quentin Nov 27 '16 at 21:48
  • `matrix` is an auto (aka local) variable. You have another kind of undefined behaviour, maybe because your compiler doesn't check the stack, so if you don't use the variable it does not crash, but as soon as you use it in the last indexes, you'll write where it's not allowed (yes Quentin is probably right: optimized out!!!) – Jean-François Fabre Nov 27 '16 at 21:49
  • @Jean-FrançoisFabre you can very well allocate a 2D array dynamically: `int (*grid)[MAX_size]; /* ... */ grid = malloc(MAX_size * sizeof *grid);` – Quentin Nov 27 '16 at 21:49
  • Yes you are right, thanks. – Tomáš Šíma Nov 27 '16 at 21:50
  • So you think that it'd be better if I removed struct from the code completely and rather defined all the variables from struct in main function? – Petr Kučera Nov 27 '16 at 21:54
  • fastest way would be to move `matrix` declaration outside the `main` function. It changes the location where your variable is allocated, and you'll have enough memory there. – Jean-François Fabre Nov 27 '16 at 21:56
  • 1
    @Jean-FrançoisFabre it *is* contiguous -- compare `int **grid` which wouldn't be. – Quentin Nov 27 '16 at 21:56
  • I just got it. Damn I'm learning stuff today :) – Jean-François Fabre Nov 27 '16 at 21:57
  • My pleasure! :) – Quentin Nov 27 '16 at 21:59
  • Okay, I ll probably try to do the declaration outside `main` function. At least I'll learn something new. Thank you :) – Petr Kučera Nov 27 '16 at 22:02