Loop-invariant Code Hoisting

We often face such a scenario in writing code: The flag is usually a constant, for example, an argument (e.g. true/false; 0/1) we passed to this function at runtime. The functions do_something() and do_something_else() may also be some code blocks and they’re almost identical. Well, if we know the flag will definitely not change within …

Zero Pointer Dereference, Huh?

Consider the following code: So, do you think the above two highlighted lines will crash the program? Well, at least to me, it will, since they’re dereferencing null pointers. Take, int b = (int) &((struct s *)0)->m2;, for example, we first dereference the zero pointer to get the member m2, and then obtain its address. …

The Bizarreness of Optimization

Background: consider the hashtable with closed addressing/separate chaining: Typically, we can have two different schemes to link through all the nodes. One is to link the last node of the current bucket to the first node of the next non-empty bucket. See the above picture and imagine taht there are links among these buckets. Another …

How To Implement A Real Emplace Routine

Above is a piece of code that attempts to implement the emplace family routines. One can insert something like const value_type& (value_type&&) val or variadic arguments Args&&… args, both of which then can be handled by the insert_leaf_at subroutine Of course, this simple implementation is flawed as it requires an extra move operation. In order …

A Running Example

I made a test in Dev-C++ (version 5.7.1, with MinGW GCC 4.8.1 32-bit), and in debug mode, via the CPU Window it produced the following instructions: procedure main: subroutine add: Following these instructions, I drew a simple diagram (partial, incomplete): The arguments for printf were left out in this diagram for simplicity, but it is …