ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

Array declaration outside vs inside of main
Posted by Lucas 3 Jan 2017 07:45
What's the difference in memory or similar between the next two codes when i create a new array, cause in the first case (array inside main) the message of jugde is "Run time error/Stack overflow" and in the second case (array outside main) the jugde accept the code:

Case 1:

#include <stdio.h>
#include <math.h>
int main(){
    double array[128*1024];
    int i = 0;
    unsigned long long n;
    while (scanf("%llu", &n) != EOF) {
        array[i++] = sqrt(n);
    }
//Code for print......



Case 2:

#include <stdio.h>
#include <math.h>
double array[128*1024];
int main(){
    int i = 0;
    unsigned long long n;
    while (scanf("%llu", &n) != EOF) {
        array[i++] = sqrt(n);
    }
//Code for print......

Re: Array declaration outside vs inside of main
Posted by ToadMonster 3 Jan 2017 15:05
In example 1 array is allocated on stack, in example 2 - on global memory
http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c

My opinion:
- stack is small;
- stack size is unpredictable.
Conclusion: there shouldn't be big variables on stack. There shouldn't be recursion with more than logarithm complexity. If recursion depth is above 100 then it isn't usable for practical purpose.

Note: STL containers aren't big despite dozens of elements inside.
They use heap to hold elements and don't spend too much stack size.
So here is ok:
int main() {
    std::vector<double> array(128*1024);
...


Re: Array declaration outside vs inside of main
Posted by Lucas 5 Jan 2017 06:49
Thank you very much for the clarification..and link of course.