In this article, I’m going to talk about some practical uses, practical motivations, and common misunderstanding for move semantics. I’ve seen a lot of posts talk about move semantics in a very contrived way, and I’m here to make things more practical. If you’re a beginner to move semantics check this post out and come back. Also, a lot of these code snippets are similar so don’t be intimidated!

Our first example is going to look at vectors. Here’s a quick string and vector implementation.

I want to draw your attention to vector::resize . This is when the vector…


In this article, we’re going to dive deep into how C++ inheritance looks in memory and how polymorphism works under the hood. This is not an article on best practices and motivations for inheritance, but rather how C++ makes such powerful and fast inheritance tools.

Let’s start with single inheritance.

The Derived class inherits all the member variables and functions of Base, and the objects memory looks like this

The Derived object looks similar to the Base object in memory in the beginning, but then has some extra things that it created like int b.

You might also be…


Photo by Alexandre Debiève on Unsplash

Let’s talk about all the complex things that go into actually executing simple C++ code. We’re going to talk about memory, compilers, assembly code, and more, so buckle down and let’s see how this executes.

struct A {
public:
A(int _a, char _b, double _c): a(_a), b(_b), c(_c) {}
int foo(int i);
int a;
char b;
double c;
}
int main(){
A A1(1,'c',146.2);
cout << A1.c;
cout << A1.foo(3);
}

1. We create a new stack frame by assigning our stack frame register, S=0.

2. We allocate 16 bytes of memory on the stack for A1. An int has 4…


When you use queues in a concurrent program, you would usually synchronize push and pop functions with locks. You’d lock before pushing or popping and unlock right before the function ends. However, locking comes with some overhead in both creating and acquiring them, so it would be amazing if we could avoid using locks all together and still support concurrent operations. For many data structures like linked lists, stacks, and queues, we can make synchronized, lock-free versions of them with the help of the atomic function Compare and Swap (CAS). In C++, its called std::atomic<T>::compare_exchange_weak.

What is CAS?

CAS was a CPU instruction…


The std::unordered_map in C++ standard library is a really powerful data structure offering insertion, deletion, and lookup in O(1) amortized time. std::unordered_map is implemented as a hashmap or hashtable which are the same thing. This article is going to describe how the C++ standard library implements the unordered_map, how it achieves those time complexities, and when to use it.

class unordered_map{
private:
list<pair<int,int>> *buckets;
int bucket_size;
int total_elements;
float max_load_factor;
}

std::unordered_map is an array of linked lists of pairs. They’re pairs because they store a key value pair which are both integers in our case. bucket_size is the size…


Paxos Islands

Recent archaeological discoveries on the island of Paxos reveal that the parliament functioned despite the peripatetic propensity of its part-time legislators. The legislators maintained consistent copies of the parliamentary record, despite their frequent forays from the chamber and the forgetfulness of their messengers. The Paxon parliament’s protocol provides a new way of implementing the state-machine approach to the design of distributed systems

- The Part-Time Parliament, Leslie Lamport

Lamport describes the parliament of the ancient civilization in the Greek islands of Paxos. He describes the incredibly sophisticated system that ancient Paxons used to reach congressional consensus when human messengers often…

Josh Segal

C/C++ | Computer Systems | Low Latency | Distributed Systems | Computer Networks | Operating Systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store