There are a few things about linking that I think are worthy of writing down after I studied linking from CSAPP-3e and some resources on the web. Linkage of const Global Variables const global variables in C++ have internal linkage (as declared static) unless they’re explicitly declared extern or inline (see C++17 inline variable), whereas …
Category Archives: CS Stuff
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 …
Awesome Netcat
We can turn any process into a server in an incredibly simple manner using the powerful networking utility netcat. For example, we can make a shell a server: Now let’s try to understand the above-highlighted line. Noting that a pipeline runs in parallel, cat fifo therefore outputs the content of fifo only when nc -l …
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. …
Awesome Swiss Tables
The basic idea is open addressing, but without any redirections (thus making Swiss tables very memory efficient). Also, Swiss tables store a densely packed array of metadata with each entry consisting of 1 byte (which makes the metadata table easier to fit into the CPU cache, as opposed to, say, metadata of pointers of 8 …
Efficient Ways to Sort Strings
see the details at https://stackoverflow.com/questions/6972635/efficient-string-sorting-algorithm/71349193#71349193
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 …
An Interesting C Program
gdb a.out -batch -ex ‘disassemble/s main’