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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -