Photo by Oscar Nilsson

Vector - Resource Management Allocation

Many new developers of C++ attempt to build a Vector like container as a learning process. Getting a simple version of this working for POD types (like int) is not that complicated. The next step in getting this working for arbitrary data types takes a significant leap forward in thinking in C++, especially when you start looking at efficiency and exception safety. This set of five articles looks at building an efficient Vector implementation. I show some of the common mistakes and explain why and how to resolve the problems: ...

February 27, 2016 · 11 min · Loki Astari, (C)2016
Photo by Roman Synkevych

Smart-Pointer - Constructors

This article examines constructors that are often overlooked. It examines their use cases and explains why the added functionality is meaningful in relation to smart pointers. Default Constructor Most people remember the default constructor (a zero-argument constructor), but it is sometimes overlooked. The default constructor is useful when the type is used in a context where objects of the type need to be instantiated dynamically by another library (an example is a container resized; when a container is made larger by a resize, new members will need to be constructed, it is the default constructor that will provide these extra instances). ...

January 23, 2015 · 7 min · Loki Astari, (C)2015
Photo by Ken Blode

Smart-Pointer - Shared Pointer

So in the previous article, I covered a basic unique pointer where the smart pointer retained sole ownership of the pointer. The shared pointer (SP) is the other common smart pointer we encounter. In this case, the ownership of the pointer is shared across multiple instances of SP, and the pointer is only released (deleted) when all SP instances have been destroyed. So, not only do we have to store the pointer, but we also need a mechanism to keep track of all the SP instances that share ownership of the pointer. When the last SP instance is destroyed, it also deletes the pointer (The last owner cleans up., A similar principle to the last one to leave the room turns out the lights). ...

January 15, 2015 · 7 min · Loki Astari, (C)2015
Photo by ThisisEngineering RAEng

Smart-Pointer - Unique Pointer

On codereview.stackexchange.com in the C++ tag, it seems like a write of passage to implement your own version of a smart pointer. A quick search brings up the following: 02/Sep/2011 - shared_ptr implementation 26/Nov/2011 - Shared Pointer implementation 18/Apr/2013 - Request for review: reference counting smart pointer 20/May/2013 - Efficient smart pointer implementation in C++ 11/Aug/2013 - C++98 Unique Pointer Implementation 14/Aug/2013 - I wrote a class to implement auto_ptr 28/Aug/2013 - yet another shared pointer 04/Mar/2014 - Smart pointer implementation 13/May/2014 - One more shared pointer 14/Jun/2014 - Is this a meaningful Intrusive Pointer Class? 04/Aug/2014 - Simple shared pointer 08/Oct/2014 - Smart but simple pointers 15/Nov/2014 - Simple auto_ptr 19/Dec/2014 - Yet another smart pointer implementation for learning Writing your own implementation of a smart pointer is a bad idea (IMO). The standardization and testing of smart pointers was a nine year process through boost, with boost::shared_ptr and boost::scoped_ptr, finally resulting in the standardized versions being released in C++11: std::shared_ptr and std::unique_ptr. ...

December 30, 2014 · 8 min · Loki Astari, (C)2014
Photo by Possessed Photography

Nearly New Year/New Resolution

New Year/New Resolution They say you can only get better at something by doing it. I want to improve at writing articles about my work, so I better keep trying. After reading the articles I wrote last year (with the hindsight of a year), I realized that my pros seem a bit dry and laborious. So, I will try again with an eye on fixing this problem. My two main inspirations are colleges that do this rather well: Kate Mate, a highly talented manager whose opinions I respect, and Rand Fiskin, the Wizard of Moz (though I am not sure he uses this title anymore). Both are highly respected in their fields and can write comprehensively and interestingly about subjects.

December 6, 2014 · 1 min · Loki Astari, (C)2014
Photo by Matthew Brodeur

Control Flow

So far, we have created basic programs that perform a single task without making any decisions. Most (all but the most trivial) programming languages provide decision-making constructs (Conditional Branching). C++ provides two forms of branching. The “If Statement” and the “Switch Statement” . Note: Looping is also a form of branching. The looping concept is extensive enough that we will deal with looping separately in its own article. If Statement The “If Statement” allows code to be executed when a specific condition is fulfilled and optionally an alternative piece of code otherwise. ...

December 2, 2013 · 6 min · Loki Astari, (C)2013
Photo by Alex Knight

Switching to OctoPress and GitHub

I have not blogged much, until recently, so I am not an HTML/CSS/Javascript expert. Thus, layout, or layout during writing an article, is not of supreme importance to me. I expect the framework to handle that all for me. But that was my issue with WordPress. As a normal blogger I am sure it is not an issue, but the tools for blogging about code are rudimentary and not well integrated in to WordPress; forcing me to write in HTML (see Set up WordPress). I write a lot on other sites that specialize in coding and these sites have developed a style called <MarkDown>. The two most common versions are ‘StackOverFlow markdown’ and ‘GitHub markdown’. ...

November 30, 2013 · 4 min · Loki Astari, (C)2013
Photo by Ben Griffiths

Functions

Usage All C++ applications must have at least one function called main(). Additionally, you can have user-defined functions that encapsulate individual tasks, thus making the code cleaner and easier to read. Therefore, this is a useful feature if you repeat the same task many times with only slight variations: function1.cpp #include <string> #include <iostream> int main(int argc, char* argv[]) { std::cout << "What is your first name?\n"; std::string firstName; std::cin >> firstName; std::cout << "What is your second name?\n"; std::string secondName; std::cin >> secondName; std::cout << "What is your Mother's name?\n"; std::string motherName; std::cin >> motherName; std::cout << "What is your Father's name?\n"; std::string fatherName; std::cin >> fatherName; } The obvious repetition here is easy to spot. We can simplify this code using a function that does all the common work. We can pass anything unique as a parameter to the function. ...

November 24, 2013 · 4 min · Loki Astari, (C)2013
Photo by Oscar Nilsson

Variables

In most programming languages, you have the concept of variables. These are named objects that hold a value (more formerly referred to as state). By manipulating a variable, you manipulate the state of the object the variable refers to. add.cpp void addFunction() { int x = 0; // Declare (and initialize) a variable called "x" x = x + 5; // Manipulate the variable "x". // The variable "x" now holds the value "5" int y = x + 3; // Declare (and initialize) a variable called "y" // This will take the value "8" by adding "+" 3 // to the value of "x" } C++ is a strongly typed language. This means each variable has a specific type that does not change (above that type is int). The operations that can be performed on an object depend on the object’s type, and the operation’s result can depend on the types involved. C++ has several built-in types (listed below) but allows the definition of new user-defined types (which will be described in a later article). The standard library provides a set of commonly used standard types (listed below). ...

November 19, 2013 · 3 min · Loki Astari, (C)2013
Photo by Roman Synkevych

Common Mistakes

1: using namespace Every new developer who comes to C++ always starts writing code like this: myfirstprog.cpp #include <iostream> using namespace std; It seems reasonable, and every book on learning C++ perpetrates the same mistake. The problem is the using namespace std;. In programs that are only 10 lines long (like in most books), it does not cause any problems. But as soon as your code strays to any meaningful size, it becomes an issue. The problem with teaching new developers this technique is that they are unaware of the problems it causes, so it becomes a habit for all code they write. Break this habit now before you start doing it without thinking at the top of every source file you write. ...

November 18, 2013 · 4 min · Loki Astari, (C)2013