Rafael Baptista

Generating 64-bit Random Numbers in a Given Range

// 2012-05-14 05:00:00

Generating 32-bit numbers in a range, that is given a 'high' and a 'low' number is straightforward in 32 bits, if you have access to a 64-bit register. The key is multiplying the result by the difference between high and low, and keeping just the bits above 32.

Doing this for a range above 64 bits gets tricky because you're back to needing to multiply by 2 64-bit registers.

The solution is simple. Implement a 64-bit multiply that keeps only the top 64 bits of the resulting 128-bit quantity.

Using BerkeleyDB to Store Serialized Objects in C++

// 2012-05-10 05:00:00

We create an interface that allows me to store serialized C++ objects in a database. Each object it flattened into a text buffer, and used as either a key or a value in a Berkeley DB table. In addition we want to use all the features of the database, including ACID, and transaction processing.

We're using Berkeley DB, instead of a database server like MySQL because implemented correctly it can be much faster. When you use a database server you have all the overhead of network operations. In addition, with BerkelyDB it is much simpler to put binary objects into the database, and still access the data elements in your structures as database keys. With a straight SQL implementation, anything you want to use as a key needs to be extracted as a separate database record field.

Link Errors Compiling MySQL C++ Programs on 64-Bit Windows

// 2012-05-09 05:00:00

Usually this problem is caused by not linking with the mysql libraries at all. You have to make sure that you link with libmysql.lib and mysqlclient.lib. But if you are on 64-bit windows, and install the 64-bit binaries, and then try to build 32-bit programs, you will get these link errors even if you link with the 64-bit libraries.

The answer is to download the 32-bit installer, and extract the 32-bit libraries. The 32-bit libraries can still communicate with a 64-bit database server.

Generate Stack Traces on Crash Portably in C++

// 2012-05-05 05:00:00

When a program crashes the operating system will sent it a signal, to give it one last chance to wrap up what it is doing, before the OS terminates the process. You program can intercept the signal, and run code. You could for example try to recover from the error. In this case all we want to do is print out a stack trace.

Trapping the signal is pretty straighforward. But first a little housekeeping. We're going to create this system as a trivial c++ class that only has a constructor: