some no-NOOP’s

Sometimes every programmer finds something in the code that shouldn’t do anything, but in reality is essential to the correct functioning of the code because of some side effect. Someone really should make a site that gathers these since they are usually really interesting and you can learn much from the reasoning behind them.

One of the first of encounters with these subtle no-NOOPs was back when I was at university and had to hack something together for a deadline that was just a few hours away. The program worked fine under the FreeBSD that I was using for development but crashed under the Solaris where it was supposed to be run by the supervisor.
The bug seemed to be a Heisenbug since it went away when I added printf()’s to see what’s happening. I inferred that it must be something that corrupts the stack and I added some debug variables along with printf’s. These must have provided enough buffer space on the stack to avoid overwriting critical stuff.

Since I didn’t have any time left before the deadline to actually debug it I just added declaration of a small char array and set some of it’s element so the compiler wouldn’t complain that it was unused. This stabilized the program even though in a extremely ugly fashion. Of course I also mentioned this problem in the report and fixed the real bug later.

The next one is probably known to every embedded programmer. If you want to wait for something for a very small amount of time and do not have any operating system that would provide you with sleep() or other similar facilities the logical thing is to do a busy wait – basically a simple loop similar to:

for(int i=0; i<1000; i++);

The only problem was that it didn't work. It took me almost an hour before I was desperate enough to check the compilers assembler output and saw that the loop was just optimized away since compiler inferred it wasn't necessary.

The next one comes from a embedded platform browser that I use at work. All the pages that used the device specific JS APIs had the following line as the first JS line on every page:

var v = document.width;

The v variable was never used anywhere, but the code wouldn't work without that line in place since some of the platform specific objects were missing.
So executing that line probably initialized JS engine into some good state.

And the last one is from the wonderful world of JS where one of the things you have to do to work around some of the same domain origin policy limitations is this:

document.location = document.location

I have read the explanation several times and I still can't fully understand why would it change anything.

Leave a Reply

Your email address will not be published.


*