What is wrong with this code? (variable argument lists in C++)

Started by
9 comments, last by frob 16 years, 3 months ago
Quote:Original post by Antheus
Quote:Original post by frob
Bug: if begin == end, you divide by zero. This is undefined behavior. You should detect the condition and do something sane.

Average of zero numbers is undefined per se, and should trigger a division by zero exception.

There's nothing else you can return in this case, at least not with function in this form.

This is C++, so that is unequivocally false.

Division by zero does *NOT* generate an exception. It is undefined behavior.


Most systems will call abort() or terminate(), bringing your app to a screeching (and irrevocable) halt. Microsoft C++ will often use their own SEH exception, but it can be disabled or ignored. On several game consoles it will cause the machine to crash.

Undefined behavior is a really bad bug, you shouldn't just write it off even when it happens to work on your particular system. In this case, you should probably throw your own exception.

This topic is closed to new replies.

Advertisement