Avoiding deadlocks the C++ way

When interviewing engineers for a C++ programming position the question of deadlocks often comes up (ask me how I know this 😉 ). What is it? And how to avoid it? Often times a deadlock occurs due to a wrong order of acquiring locks: multiple threads need to access 2 or more shared resources; each […]

Multi-hashing

Yes I totally invented this term 😛 What I mean by it is producing multiple hashes from a single key. Like this (if the syntax is unfamiliar to you read this): Or like this (for non-template version which returns a vector): Why? One place where I needed such sorcery was my bloom filter implementation. The […]

#pragma

There are two useful directives I like to use in my code: one let’s the preprocessor know that you want to include a header fine only once, and another deals with structure packing. Instead of using the header include guards, which are ugly as sin, use at the beginning of your header files, like this: […]

Exception safe assignment

Longer title: exception safe assignment operator of resource owning objects. Uff. Because the object owns a resource, how do we write an exception safe assignment operator which will have to free up the old and allocate the new resource. By exception safe I don’t mean that it will never throw, that’s not possible. Instead, I […]

Hashing the C++ way

Modern C++ brought us template (read more about it here). In short: it’s a stateless function object that implements which takes an instance of a type as parameter and returns its hash as . It has specializations for all primitive types as well as some library types. You can also specialize it yourself for your […]

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 to 16 bytes you had to do this: Not any more! Modern C++ introduced a keyword just for that: (read more about it here). Now you can specify struct’s alignment like this: […]

Simple file I/O

I was playing around with file I/O the C++ way and decided to create a file hashing program using 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 very […]

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: 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. – when specified with a function […]

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 block. So instead of writing this: You can write this: The meaning of the two functions is identical. Notice here I’m swallowing the exception instead of propagating […]

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

No, not single sign-on 😛 Short string optimization. But before I get into that, little rant… F**K Windows 😡 ! I’ve tried, I really tried to build all my coding examples on Windows. I give up. Even with vcpkg I can’t get cmake to build my $hit! Why is it so easy on Linux with […]

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 to […]