c++ - No Runtime error after divison by zero -


i using microsoft visual c++ 2010 , have following code in divide 1 0 see

#include <cstdio>  int main() {       int x;    x = 0;    1/x;     while (1) {       std::printf("running!!\n");    } } 

and surprisingly don't find run time error , program continued execute , displays running!!

so question why "1/x" not considered run time error , , why program don't stop?

in complement else, notice comments "getting or not run-time error" makes me thinking misunderstanding of ambiguous terminology.

in common computer science "runtime error" no more computer language of human brains (plain english) means: , error noticed during "program execution".

so yes, getting dump os after signal "runtime error". @ least english language.

but has noting std::runtime_error std::exception thrown standard library (or whatever other code based on standard library) when error fund in own code.

also term "exception" ambiguous: in os terminology "rescue code" appropriate os driver put in answer cpu hardware trap. in c++ is either base class representing standard library errors or whatever value subject of throw statement.

the point, here, integer division 0 not standard library implementation detected error: basic integer arithmetic considered c++ language primitive hosting environment. in of platforms, operator/(int,int) implemented through div assembler instruction (at least on of cpu) , div 0 operand handled cpu microcode cpu exception, produce "trap" (or interrupt, or whatever platform terminology calls it) handled os (or specific os driver). there nothing in c++ compiler (and produced executable) knows what's going on during div evaluation (since internal cpu),so there no throw statement can written, , hence no std::exception (or whatever other c++ type) catch. operating system driver can replaced, , -by default- terminates application.

that's similar behavior of equivalent default catch(...) in c++ startup code invokes main (that calls exit), hence source of confusion.

to complicate more things, compiler optimization can discard whatever operation not producing used result, making become not visible.

so, output division result must let compiler not discard operation(s). , when done, cpu trap producing os signal observed. -in plain english "error @ run-time", not c++ std::runtime_error since no such throw statement exist in division operator implementation.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -