C: Array initialization segfaults depending on size and call to printf() -
another student asked me wrong c code. reproduced erroneous behavior , have no idea why segfaults. consider tiny c programm:
#include <stdio.h> int main(void) { int n = 590; double a[n][n]; double b[n][n]; double c[n][n]; printf("done"); }
- set
n
value <= 590:
runs without errors, or without output. - set
n
value > 590:- runs flawlessy output line removed.
- compile , run output: segmentation fault
what's reason this? can explain?
you try allocate more memory it's available on stack causes stack overflow. better allocate huge arrays dynamically using malloc
, calloc
or realloc
. don't forget free memory calling free
when finish :)
these questions too:
c/c++ maximum stack size of program
segmentation fault on creating array in c
segmentation fault when using variable initiate array
Comments
Post a Comment