Data alignment the C++ way

Before modern C++ the only way to align variables or structures on a given byte boundary was to inject padding; to align a struct to 16 bytes you had to do this:

Not any more! Modern C++ introduced a keyword just for that: alignas (read more about it here). Now you can specify struct’s […]

Simple file I/O

I was playing around with file I/O the C++ way and decided to create a file hashing program using ifstream and Botan crypto library. The program reads an entire file specified as the command line argument and takes the SHA1 hash of the content. It’s amazing what you can accomplish with well designed frameworks in […]

Better bloom filter

Based on this implementation it supports multiple hashes for better positive hit ratio. Can be initializes with size in bits and number of hashes to perform, like this: bloom_filter bloom(128, 5);As always, complete implementation on GitHub: bloom.hpp.

Bloom Filters

From Wikipedia: A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. In other words, given a set of elements, bloom filter can tell you that: A) given element is definitely not in the set, or B) given element is maybe in the set. […]

C++ Attributes

C++11 introduced standard attributes: a way to mark fragments of code with useful information for the developer or optimization information for the compiler. See a complete list of standard attributes here, Clang attributes here, and Microsoft attributes here. I will go over a few of them in this post. [[nodiscard]] – when specified with a […]

Initialization list exceptions and raw pointers

What to do when an exception is thrown on the initialization list when allocating memory for a raw pointer? The situation is easy if your class only has one raw pointer member, but it gets complicated with two or more. Here’s a code example that’s guaranteed to leak memory if the second new int throws an […]

Function try-catch blocks

Syntactic sugar or a useful feature? More than just sweet sweet sugar baby! This little known feature is a nice way of wrapping an entire function in a try catch block. So instead of writing this:

You can write this:

The meaning of the two functions is identical. Notice here I’m swallowing the […]

The #1 rule of cryptography

The #1 rule of cryptography: Don’t invent your own! OK wiseman, now what? You want to add crypto to your program but you don’t want to code it all yourself. I’ll show you three libraries that make it possible. The choice will be yours as to which one to use. For this example I wanted […]

{fmt}

I found this cool little text formatting library with very clean interface and wanted to share it with you. I decided the best way to introduce it to you is not through an extensive tutorial but rather code which illustrates how to use it; so I wrote a program which does the same thing in […]

SSO of std::string

What is short/small string optimization? It’s a way to squeeze some bytes into a std::string object without actually allocating them on the heap. It’s a hackery involving C++ unions and clever space management. Say sizeof(std::string) is, oh I don’t know, 24 bytes on Mac’s LLVM? The implementation manages to squeeze 22 characters into that (not […]

HTTP queries

Today I want to show you how to use cURLpp (C++ wrapper around libcURL) to make a simple HTTP query to ip-api.com in order to retrieve geolocation information of a given host or IP address. I chose cURLpp because it’s simple and easy to use; the example program would not have been any harder using […]

C-style callbacks and lambda functions

You can use a non-capturing lambda function with C-style APIs that expect a function pointer. As long as the signatures of the callback and the lambda match, the lambda will be cast to a function pointer (or you could define a “positive lambda”, one with a + in front of it; this causes automatic conversion […]

Propagate exceptions across threads

What if you need to catch an exception in a worker thread and re-throw it in the main thread that’s waiting for the worker to finish? std::future works this way. If you spawn a future on a new thread using std::async(std::launch::async, ...); and that future’s worker throws an exception, when you later call get() on the […]

int main()

I have been spanked by certain commenter (who shall not remain anonymous 😉 ) on here and FB about my style of naming unused main arguments and unnecessary return 1; at the end of every main function. I have though about and I… concede the point of his argument 🙂 From now on the style on […]

XML-RPC

XML-RPC is yet another method of implementing remote procedure calls. It used XML over HTTP to transmit data. In my past live working at TLO I used XML-RPC-C library to implement communication between cluster nodes and a cluster management system. I thought the library was well designed and easy to use so I wanted to […]

Base64 encoding

Base64 encoding: turning binary data into ASCII text for the purpose of saving it to text files like XML, or transmitting it over protocols like HTTP, or embedding it into web page files, and many other purposed. That’s the basic idea behind it. For every 3 bytes of input you get 4 bytes of output, […]

Extremely Fast Compression Algorithm

LZ4. GitHub repository here. It is open-source, available on pretty much every platform, and widely used in the industry. It was extremely easy to get started with it. The C API could not possibly be any simpler (I’m looking at you zlib 😛 ); you pass in 4 parameters to the compression and decompression functions: […]

Parsing command line options

In case you haven’t noticed, I love Boost 😛 so I’m going to introduce you to its Program Options library. It is a command line options parser; it can also read options from an INI file, and it works with STL containers. It supports showing a help message, setting default values for missing options, allows […]

ANSI escape codes

ANSI escape codes are a way to do more than just plain text in the terminal (be it Windows cmd.exe or UNIX xterm). A picture is worth a thousand words so here’s what I was able to do with them: All of the text appearance manipulation and coloring was done using a tiny library I […]

Two-factor authentication

If you don’t know what multi-factor authentication is please read this before continuing. I am going to assume you understand the security concepts mentioned in this post… In plain English: two-factor authentication is something you know (your password) and something you have (your token). I will focus on the token in this post. Apps like […]