c++ - Declare variable in global/function scope. Stack difference? -
why there different stack variables if declare them in global or function scope? 1 of 2 example crashes, because of stack overflow. one, define variable inside scope.
does crash:
constexpr size_t max = 1000000; // customise int main() { int arr[max]; return arr[max - 1]; }
does not crash:
constexpr size_t max = 1000000; // customise int arr[max]; int main() { return arr[max - 1]; }
info: cygwin, gcc 4.9
edit: know, second example have memory in data segment. how big can data segment be? big heap area?
the first 1
constexpr size_t max = 1000000; // customise int main() { int arr[max]; return arr[max - 1]; }
you declare array in function, goes stack limited , cause stack overflow.
the second 1
constexpr size_t max = 1000000; // customise int arr[max]; int main() { return arr[max - 1]; }
you declare @ global, should accesible between function goes heap (rather big). not using stack here.
source : static , global variable in memory
Comments
Post a Comment