ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1001. Обратный корень

Array declaration outside vs inside of main
Послано Lucas 3 янв 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
Послано ToadMonster 3 янв 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
Послано Lucas 5 янв 2017 06:49
Thank you very much for the clarification..and link of course.