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

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

Runtime error (access violation) for C
Послано Mozzi 13 сен 2017 23:37
 int main(){
    long long a;
    double b=0;
    int count=0;
    double *array = (double *)malloc(sizeof(double));
    while(scanf("%lld",&a)){
        b = sqrt(a);
        array[count]=b;
        count++;
    }
    for(int i = count-1; i >= 0; i--){
        printf("%f\n",array[i]);
    }

    return 0;
}



my code is showing above, but each time it returns the runtime error. i run it on my PC but everything is fine. is there any runtime error here?

thanks!
Re: Runtime error (access violation) for C
Послано Mahilewets Nikita [BSUIR] 14 сен 2017 08:43
There is access violation
Your array is only ONE element in size
It works on your PC because your runtime environment does not checks whether you are violating borders of memory

Memory allocation is just a promise to use allocated memory

But in C++, there are no checks by default

So it is programmer's task to ensure there is no BUFFER OVERRUN.
Re: Runtime error (access violation) for C
Послано Mahilewets Nikita [BSUIR] 14 сен 2017 08:44
So malloc(sizeof(double) *N)
returns memory for N doubles
In your case N is just one
Re: Runtime error (access violation) for C
Послано Mahilewets Nikita [BSUIR] 14 сен 2017 08:46
When you allocate memory
System is not permitted to use that memory for it's operations
Because if it uses
You may change system parameters and break down the system