July 2023

Object Ownership -- Ilya Doroshenko

objectownership-doroshenko.pngThis article goes over the spicy topic of object ownership. We covered the lifetime quirks, and we found out that manual memory management can be a nightmare, we new and delete in the correct order. There must be something better than that. Well, there is but it comes with its own can of worms.

Object Ownership

By Ilya Doroshenko

From the article:

Since we know the rules of new and delete, namely new allocates and delete destroys, we never really cared about who is responsible for the object. This caused a lot of confusion in the past. For instance, some API codes from Win32 return strings that should be LocalFree()d, like FormatMessage or GetEnvironmentStrings. POSIX, on the other hand, has strdup as a common example of you should free it yourself. This model is confusing because you may have a lot of return statements, before which you should always call free or delete, depending on the operation. However, we have RAII since the very beginning of C++, which adds constructors and destructors. So, in 1998 resourceful people decided to add auto_ptr to the standard.

The premise was simple:

  • a simple explicit constructor, that took raw pointer from new
  • destroyed by either an explicit release/reset or a destructor on the end of the block

This was the first attempt at a structured cleanup. As time passed, the initial point began to crumble. More issues arose and the question came up: Who owns the data?

 

 

CppCon 2022 C++ Coding with Neovim -- Prateek Raman

prateekraman-neovim.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

C++ Coding with Neovim

by Prateek Raman

Summary of the talk:

If the command line has ever piqued your interest, specially using Vim/Neovim for C++ coding, but you have some reservations about losing access to IDE like features, wonder no more. This talk will present a productive C++ development environment on the Command Line using Neovim and LSP. LSP (Language Server Protocol) is the protocol which powers semantic auto-completion and intellisense in modern IDEs and we'll learn how Neovim's built-in LSP client integrates seamlessly with language servers to provide a rich editing experience.

Neovim is a modern editor built on top off the giant shoulders of Vim, and lets us have all the speed and efficiency of Vim as we edit code, while also providing rich support for C++ coding with intellisense and auto-completion via LSP. Along the way we'll also demonstrate Neovim's rich LSP aware plugin ecosystem for project navigation and IDE like workflows, all on the command line.

Even if you're comfortable with Vim/Neovim, but would like to learn how to level up the experience with modern IDE features, you will find this talk interesting.

Parsing Time Stamps Faster with SIMD Instructions -- Daniel Lemire

In software, it is common to represent time as a time-stamp string. It is usually specified by a time format string. Some standards use the format %Y%m%d%H%M%S meaning that we print the year, the month, the day, the hours, the minutes and the seconds. The current time as I write this blog post would be 20230701205436 as a time stamp in this format. It is convenient because it is short, easy to read and if you sort the strings lexicographically, you also sort them chronologically.

Parsing Time Stamps Faster with SIMD Instructions

by Daniel Lemire

From the article:

You can generate time stamps using any programming language. In C, the following program will print the current time (universal, not local time):

We are interested in the problem of parsing these strings. In practice, this means that we want to ...

 

AddressSanitizer continue_on_error -- Jim Radigan

jim-300x300.jpgVisual Studio 17.6 comes with new functionality in the Address Sanitizer runtime which provides a new “checked build” for C and C++. This new runtime mode diagnoses and reports hidden memory safety errors, with zero false positives, as your app runs.

AddressSanitizer continue_on_error

by Jim Radigan

From the article:

C++ memory safety errors are a top concern for the industry. In Visual Studio 17.6, we deliver a new experimental Address Sanitizer feature: continue_on_error (COE). We’ll remove the experimental label in 17.8. You compile as before, by simply adding the compiler flag -fsanitizer=address. With 17.6 you can enable the COE functionality by setting environment variables from the command line.

CppCon 2022 C++23 - What's In It For You? -- Marc Gregoire

marcgregoire-wiify.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

C++23 - What's In It For You?

by Marc Gregoire

Summary of the talk:

C++23, the next release of the C++ standard, is scheduled to be released in 2023. The update introduces new features to the core language and to the Standard Library. If you want to stay up to date with the latest features that are upcoming with this new release, then this session is for you.

The session includes core language topics such as consteval if statements, multidimensional subscript operators, decay copy, unreachable code, and more. New Standard Library features that will be shown include monadic operations for std::optional, std::flat_map, std::flat_set, a stacktrace library, changes to the ranges library, improvements to std::format, std::expected, and many more.

The session will include references to other sessions at CppCon, if applicable, for more deep-dive information on any particular topic.

Monitor Object -- Rainer Grimm

concurrentarchitecture-grimm.pngThe Monitor Object design pattern synchronizes concurrent member function execution to ensure that only one member function runs within an object at a time. It also allows an object's member functions to schedule their execution sequences cooperatively.

Monitor Object

by Rainer Grimm

From the article:

Problem

If many threads access a shared object concurrently, the following challenges exist.

  • Due to the concurrent access, the shared object must be protected from non-synchronized read and write operations to avoid data races.
  • The necessary synchronization should be part of the implementation, not the interface.
  • When a thread is done with the shared object, a notification should be triggered so the next thread can use the shared object. This mechanism helps avoid and improves the system's overall performance.
  • After the execution of a member function, the invariants of the shared object must hold.

Solution

A client (thread) can access the Monitor Object's synchronized member functions, and due to the monitor lock, only one synchronized member function can run at any given time. Each Monitor Object has a monitor condition that notifies the waiting clients.

 

CppCon 2022 The Most Important Optimizations to Apply in Your C++ Programs -- Jan Bielak

janbielak-mostimportant.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

The Most Important Optimizations to Apply in Your C++ Programs

by Jan Bielak

Summary of the talk:

Writing efficient programs is hard. This is because it requires a lot of knowledge, experience and strategic thinking. There have been many talks on optimization and often each addresses a single concept. Being able to achieve a bird’s eye view of factors affecting performance often requires many hours of researching the topic. To lessen the mental burden of optimizing programs, I have picked out the techniques, I believe are most important. During the talk, I will present them in an organized manner and provide practical examples of how they can be applied.

I will first discuss what I believe are the main goals efficient programs strive to achieve. Then, I will present the general methods of achieving those goals. Then, for the majority of the talk, we will discuss a few dozen performance opportunities. For each of them, I will explain the underlying mechanism of how the optimisation works. I will avoid bluntly giving guidelines to follow without explanation. Each of the techniques naturally comes with its costs, and those will be discussed as well.

I will additionally discuss various performance pitfalls. These are sometimes called “premature pessimisations” in contrast to the often used term of “premature optimizations”. I will show examples of optimizations which do not incur any cost on program readability or maintainability and as such should be considered performance best practices. Avoiding their use doesn’t improve code in any manner, while making it slower.

This talk is intended for a diverse audience, as after all, probably most of the C++ community is interested in performance. It is appropriate for hobbyists and professionals alike, with varying experience with the language, due to the gradual increase in difficulty of examples. It will be a time productively spent.

Finite State Machine with std::variant -- Bartlomiej Filipek

In this blog post, I’ll show you how to convert a “regular” enum-style finite state machine into a modern version based on std::variant from C++17. This technique allows you to improve design, work with value types and enhance code quality.

Finite State Machine with std::variant

by Bartlomiej Filipek

From the article:

Let’s start with a basic example:

  • we want to track a game player’s health status
  • we’d like to respond to events like “Hit by a monster” or “Healing bonus.”
  • when the health point goes to 0, then we have to restart the game only if there are some remaining lives available

Here’s a basic diagram with states and transitions:

CppCon 2022 Modernizing SFML in Cpp -- Chris Thrasher

christhrasher-modernizingsfml.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: Modernizing SFML in Cpp

by Chris Thrasher

Summary of the talk:

Modernizing SFML, a game development library, from C++03 to C++17.

CppCon 2022 C++20 - A New Way of Meta-Programming? -- Kris Jusiak

krisjusiak-newwaymetaprogram.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: C++20 - A New Way of Meta-Programming?

by Kris Jusiak

Summary of the talk:

In this lightning talk we will explore a few template meta programming techniques which C++20 enables.