c - Getting segmentation fault while scanning an integer value -


though strange, getting segmentation fault while scanning integer value.

here program :

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main() {     int t,n,i,j;     char c;     int grid[1000][1000],right[1000][1000],down[1000][1000];     scanf("%d",&t);     printf("hello\n");     while(t--)     {         scanf("%d",&n);          memset(right, 0, sizeof(int) * 1000 *1000);         memset(down, 0, sizeof(int) * 1000 *1000);          for(i=0;i<n;i++)         {             for(j=0;j<n;j++)             {                 scanf("%c",&c);                 printf("char = %c\n", c);                 if(c == '.')                     grid[i][j] = 1;                 else                     grid[i][j] = 0;             }         }         for(i=0;i<n;i++)         {             for(j=0;j<n;j++)             {                 printf("%d",grid[i][j]);             }         }     }     return 0; } 

doing gdb shows segmentation fault @ line scanf("%d",&t);. cannot figure out how happening?

[using gcc-4.8.4 on linux 32-bit machine ]

the problem arrays: grid, right , down big fit stack.

as far reason no compile error concerned:

because there nothing wrong code in terms of syntax or semantics. linker not have problem.

the problem arises when loader tries load program , allocate memory on stack. stack 8 mb on linux systems , arrays surpass that.

you can make them static (as suggested in comments) static members allocated on bss or data segment. in reality need rethink if need such big arrays.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -