Posts

Showing posts from February, 2018

Why and when to use constexpr

In today's article I just want to briefly cover why and when you would use the keyword constexpr. Constexpr was introduced in C++11 and is used to tell the compiler: it may be possible to evaluate this expression at compile time. As an example: Here we have a recursive function which calculates the Fibonacci number at position n in the sequence. Now we need to ask ourselves, can this computation be carried out at compile time? Well, the answer is yes it can, if we know the value of n at compile time. Lets take the above example a bit further, lets say we want the Fibonacci number at position 6 in the sequence: Now, to properly demonstrate the power of compile time optimisations and the importance of making use of them as best we can in our code I'm going to use a tool I started using recently called compiler explorer . Compiler explorer let's us enter code snippets and view the assembly output generated by the compiler. I won't pretend to be an expert in assembly as

RAII: Resource Acquisition is Initialisation

I recently ran into what I assume is a common problem for new C++ programmers, I needed to allocate some memory on the heap and ensure it was freed up when I was finished with it. The specific function I was working on was intended to query a service for its start type using Microsoft's QueryServiceStatus . My initial implementation was fairly horrible and prone to memory leaks as it didn't guarantee the allocated memory would get freed when it went out of scope. A colleague recommended I take a look at RAII and it's uses. Once I got my head around it I was able to apply it to my problem, and resolved the issues with my code. So, I'll try and explain it here with a simpler example. RAII stands for R esource A cquisition I s I nitialisation. The core principle of RAII is that we want to tie the lifetime of a particular resource to an object on the stack, such that when our object goes out of scope the object's destructor cleans up our resource. Here's a simpl

First Post: Learning to Code

I've always enjoyed problem solving, and coding has always been a great outlet for that. A couple of months back I made the move from QA to development in the company I work for. While I did study Computer Science at university this move still required me to sharpen my coding abilities and learn several languages and tools that I'd not had much experience with in the past. In my new role I'm working on the installer for our flagship product, which involves working with wix  and coding in C++, both of which are relatively new to me. So, that's probably enough of an intro as to where I'm at in my career, the next question is: why make this blog? Well, my reasons are self-serving. I want to use this blog as a tool to facilitate my own learning. It will allow me to put what I've learnt into my own words - something I've always found useful when learning something new. I'll likely focus most of my attention; at least initially, on my learning of C++, but th