|
|
back to boardShow all messages Hide all messages 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! 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. So malloc(sizeof(double) *N) returns memory for N doubles In your case N is just one 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 |
|
|