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

Runtime error (access violation) for C
Posted by Mozzi 13 Sep 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
Posted by Mahilewets Nikita [BSUIR] 14 Sep 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
Posted by Mahilewets Nikita [BSUIR] 14 Sep 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
Posted by Mahilewets Nikita [BSUIR] 14 Sep 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