0

The compiler gives error if any occurs...then how comes the run time error if compiler had already compiled ?

26th Jul 2017, 3:04 PM
Sabareeswari Mariappan
Sabareeswari Mariappan - avatar
3 Answers
+ 8
Consider: int array[5]; std::cout << array[7]; // The array index is clearly out of bound, the array is uninitialised and contains garbage values which may cause errors to your remaining code, but the code is correct syntax-wise. // tl;dr - There are some things that the compiler does not check. Successful compilation does not guarantee proper program flow during execution. This is why these errors are called "run-time" errors. A better analogy would perhaps be for a car to contain all required components, but incorrectly assembled during manufacturing process. Imagine, for example a car has the steering, the gears, the airbags, the seats, the engine; The 'compiler' confirms that all exist and is installed, but the compiler may not know that your steering is installed upsidedown. Driving the car into a wall would be more like input error.
26th Jul 2017, 4:34 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
To continue Conway's analogy, there might be no mechanical problem with the car - so it starts just fine. However, you can give it the wrong input and crash it into a wall... Similarly, the code could be just fine: print a/b However, b is supplied by the user - and if the user enters 0, you get a ZeroDivisionError...
26th Jul 2017, 3:13 PM
Bogdan Sass
Bogdan Sass - avatar
+ 1
Think of this as a car. The compiler only check if you car has any problem. It does not know how you will drive car.
26th Jul 2017, 3:09 PM
Conway Lih
Conway Lih - avatar