Once upon a time, every programmer has written something like this here:
double div (double a, double b){
return a / b;

He was fully convinced that this function does exactly what he need - divides a by b. But sooner or later, it turned out to be next friend or teacher, who explained that this function makes one more important thing in the life of any program: it brings down it with the exception of dividing by zero, if b makes zero. After that, the future programmer had to understand the need of validation of input data. Someone decided that this issue is settled, and someone came to the conclusion that this is only half of the matter.

In fact, put verifications at every step are a common practice. This is taught in the university. But with the time comes the idea that something is not right:
  • Verification of different things takes a lot of space in a code. Because of verifications, this code stops to be visible.
  • Profiler shows that a significant portion of time is spent on the verifications. The program is slow and users complain about it.
  • The same verifications repeat for many times. If a certain class has functions that call each other and work with the same data - verifications are written to each of them and some of them trigger “run free”.
Pirat 8 september 2011, 11:51

The other day I debugged the driver, because when I was using it, at first glance appeared chaotic and some magic BSoDs. All function calls were correct; there were not any errors with zero pointers and other common problems. I did not figure out what could happen with this driver, I asked more experienced friend to see what's wrong. A few hours later he said that understood the reason for the bug. The result confused both of us.

It turned out that the cause of falling was the banal and simple: in the logic of the driver was used a stack extensively, often a function used 10 - 20 KB of memory on the stack under various buffers and arrays. MSDN says that the kernel stack is limited to three pages of memory (for 32-bit architecture is about 12KB), and therefore it is better to refrain from the multiple function calls and recursion.
Pirat 6 september 2011, 11:11