Compile-Time and Runtime-Safe Replacement for “printf”

Thursday, January 29, 2015

C++ 11 is truly beautiful. And one of the ways in which it is beautiful is how you can implement a compile-time and runtime-safe version of the popular C runtime function printf. Originally, printf was implemented as a variadic function that parses its first argument (a format string) to determine how many additional arguments it should read from the stack. printf has no way of knowing how many parameters were actually provided; any mismatch means a nasty exception at runtime, or, worse, undesired output. For example: printf("%d"); printf("%s"); printf("%d %d", 42); printf("hmpf", 42); The first version will probably work and print some arbitrary stack value...
tags: ,
2 comments

Implementing std::tuple from the Ground Up – Part 4: Getting Tuple Elements

Wednesday, January 28, 2015

We have a constructible tuple at this point, but we don't have a way of getting tuple elements by index or in any other way. It's time to implement the main accessor -- get<> -- which we use to read and write the tuple's elements. There are two flavors of the get<> template: get by index and get by type, the latter being part of tuple's interface since C++ 14. We are going to implement the latter in terms of the former. Here are the overloads we need for get<>: template <size_t I, typename... Types> ??? get(tuple<Types...> const& tup); template <size_t I, typename......
tags: ,
no comments

Implementing std::tuple From The Ground Up – Part 3: Constructing Tuples

Friday, January 23, 2015

In the previous installment we were finally able to define what tuple derives from. As a reminder, if we have a tuple of n elements, it (indirectly) derives from n instantiations of tuple_element. For example, tuple<int, string> indirectly derives from tuple_element<0, int> and tuple_element<1, string>. We'll need to add some operations to tuple_element to make it more useful. At the very least, we need to make it constructible from its value type: explicit tuple_element(T const& value) : value_(value) {} explicit tuple_element(T&& value) : value_(std::move(value)) {} Now let's start building some fundamental operations for our tuple class so that we can get busy constructing...
tags: ,
one comment

Implementing std::tuple From The Ground Up – Part 2: Index Sequences

Friday, January 16, 2015

In the previous installment we had the following definition of tuple, and were looking for a way of improving it so that the caller doesn't have to provide the integer indices. template <size_t... Indices, typename... Types> class tuple : tuple_element<Indices, Types>... { }; tuple<0, 1, int, string> tup; Basically, we need to provide the indices ourselves. First, let's decouple the tuple class from the indices -- we don't want the client to see the additional template parameters required: template <size_t... Indices, typename... Types> struct tuple_impl : tuple_element<Indices, Types>... { }; template <typename... Types> class tuple : tuple_impl<???, Types...> { }; So... how is the tuple class going to provide tuple_impl with all the indices?...
tags: ,
5 comments

Implementing std::tuple From The Ground Up – Part 1: Introduction and Basic Structure

Monday, January 12, 2015

std::tuple is a very nice facility originally introduced in C++ TR1. It is a heterogenous container of elements that has a statically known size. In C++ 11, std::tuple can be implemented using variadic templates; a single std::tuple class can support an arbitrary number of template type arguments. In this series of blog posts we will implement std::tuple from first principles. The purpose of this exercise is not to provide the best-performing or most-conformant tuple implementation, but rather to see what foundational concepts are required to implement it. NOTE: This blog series relies on good familiarity with variadic templates and basic...
tags: ,
5 comments

Uneven Work Distribution and Oversubscription

Wednesday, October 23, 2013

A few days ago I was teaching our Win32 Concurrent Programming course and showed students an experiment with the std::thread class introduced in C++ 11. The experiment is designed to demonstrate how to partition work across multiple threads and coordinate their execution, and the work to partition is simply counting the number of primes in a certain interval. You can find the whole benchmark here. The heart of the code is the parallelize_count function, below: void parallelize_count(unsigned nthreads, unsigned begin, unsigned end) {     std::vector<std::thread> threads;     unsigned...
no comments

Searching and Displaying C++ Heap Objects in WinDbg

Monday, August 5, 2013

This is something I am pretty excited to tell you about. But first, some motivation. In managed applications, there’s a huge number of tools and ways to inspect the managed heap contents. You can use a memory profiler to see references between objects and inspect individual objects. You can use WinDbg with the SOS extension to dump all objects of a particular type and execute additional scripts and commands for each object. You can even write C# code that uses the ClrMd library to parse heap contents and write your own diagnostic tools. C++ has nothing of...
2 comments

Displaying and Searching std::map Contents in WinDbg

Thursday, July 25, 2013

This time we’re up for a bigger challenge. We want to automatically display and possibly search and filter std::map objects in WinDbg. The script for std::vectors was relatively easy because of the flat structure of the data in a vector; maps are more complex beasts. Specifically, an map in the Visual C++ STL is implemented as a red-black tree. Each tree node has three important pointers: _Left, _Right, and _Parent. Additionally, each node has a _Myval field that contains the std::pair with the key and value represented by the node. Iterating a tree structure requires recursion, and...
tags: , ,
3 comments

Displaying and Searching std::vector Contents in WinDbg

Thursday, July 18, 2013

WinDbg has never been very good at visualization. While Visual Studio has always had autoexp.dat, and as of late also native debugger visualizers, WinDbg users had to settle for dumping memory areas and searching memory to identify patterns. On the other hand, Visual Studio does not offer any automation opportunities today if you’re looking to simplify your debugging process. You can’t write macros anymore as of Visual Studio 2012. WinDbg continues to offer scripting support so you can automate any mundane debugging tasks you encounter. If you make it past the initial learning curve and master WinDbg scripts,...
tags: , ,
3 comments

Automatically Obtaining Source and Line Number from Symbol Name and Offset

Tuesday, June 25, 2013

Once upon a time, unmanaged code developers had to work very hard to correlate code offsets back to source file names and line numbers. One approach involved generating .cod files (assembly listings) for each module, and then painstakingly comparing the instruction offsets with the contents of the .cod file. For example, if you received a faulting stack trace from a client machine that had the frame BatteryMeter!TemperatureAndBatteryUpdaterThread+0xd0, you could go back to the .cod file for BatteryMeter.exe, look up the code listing for TemperatureAndBatteryUpdaterThread, and then look for the source line located at (or near) offset 0xd0. ...
one comment