Profiling resources

Tools

  • valgrind
  • perf
  • /usr/bin/time --verbose
  • Tracy
  • htop

Compiler options

Enabling in gcc

Use -p for prof and -pg for gprof. But it will take longer to run.

References

static

Non-const global variables have external linkage by default Const global variables have internal linkage by default Functions have external linkage by default What’s the “static initialization order ‘fiasco’ (problem)”? C++ scoped static initialization is not thread-safe, on purpose! (pre-C++11) constinit static vs std::call_once vs double checked locking DCLP Double-Checked Locking Pattern const and static variables don’t have external linkage. [Read More]

Favourite C++ features

And wish list

C++17 Boost filesystem execution policy: parallel algorithm support for range-based for loops - potential for easily parallelsing existing code structured bindings clamp std::optional [[maybe_unused]] C++20 spaceship operator range based for loops with initialiser bit header coroutines ranges and views - a nod to strongly typed, const languages like Haskell https://en.cppreference.com/w/cpp/utility/source_location https://en.cppreference.com/w/cpp/numeric/constants Wish list See the compiler support matrix. std::format modules - wait for clang12, gcc11 Modules Look interesting but not available until clang-12. [Read More]

C++ tricks/idioms

Get the file name out of a path Concise code to extract everything after the slash (if there is one) without checking std::string::npos. The +1 rounds a “not found” value up to zero if there’s no slash and then substr returns the original string. #include <iostream> int main() { const std::string full_path = "one/two.jpg"; const std::string just_the_file_name = full_path.substr(full_path.find_last_of('/') + 1); std::cout << "\"" << just_the_file_name << "\"\n"; } There can only be one – call a routine only once Using IIFE. [Read More]

GDB

Debugger crib sheet

Adding debug symbols gcc -g hello.c gdb a.out # Run program run # Run and immediately break start # View code at current break point list # Break main b main # Break at line 9 b 9 TUI mode - text user interface Type ctrl-X A ctrl L - repaint ctrl X2 - multiple windows ctrl X2 - view registers tui reg float - view floating registers ctrl x 1 - source code view ctrl p/n - command history Python interpreter built in (since version 7) import os print(os. [Read More]

Travis CI

Configuration for C++, Python, R, bash and Graphviz

Note: my use of Travis CI has been superseded by GitLab which has all this built in. In fact this blog is generated by GitLab. Create an account with your GitHub login and enable a repo to get started. (Travis Pro appears to enable new repos by default.) Simple C++11 compilations If you just want to get something building quickly the default Trusty build has clang 5 pre-installed, no need for complicated matrices. [Read More]

Exceptions in destructors

In C++11 destructors default to noexcept so you have to go out of your way to make them throw. See throwing a vector in a destructor. #include <exception>#include <iostream>#include <vector> // Note the braces void exception1() try { throw 1; } catch (int e) { std::cout << "Caught " << e << std::endl; } catch (const std::out_of_range &oor) { std::cout << "Caught OOR\n" << oor.what() << std::endl; } catch (const std::exception &e) { std::cout << "Caught " << e. [Read More]

STL containers

Sequence containers vector list deque array forward_list deque is not guaranteed to store all its elements in contiguous storage locations but has efficient insertion and deletion of elements at the beginning and end of a sequence. Unlike the other standard containers, array has a fixed size. Forward lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence. forward_list has been designed with efficiency in mind. [Read More]

Preparing for a C++ interview

As a senior software engineer

The Amazon tech interview topics is a great guide to whet your appetite. Know your language standards, algorithms and data structures. Be comfortable calculating and discussing the complexity of your solutions (Big O notation). What’s the difference between symmetric and asymmetric encryption? Get some side projects on GitLab to talk about at interviews and run/deploy them as a daily cron job. Be aware that recruiters may remove any contact details in your CV – including URLs with your coding projects – so be sure to obfuscate them. [Read More]

ADL

Argument dependent lookup

Why does this compile? A one-liner to print a vector to stdout. But why does this compile without the namespace std::? #include <iostream>#include <vector>#include <iterator> int main() { const std::vector<int> v{1, 2, 3, 4, 5}; copy(cbegin(v), cend(v), std::ostream_iterator<int>(std::cout, "\n")); } Run the code on Compiler Explorer, Jason Turner on YouTube and ADL on Wikipedia. Note: C++14 is the default for the latest GCC (9.2). Exercise Propose some other examples of ADL. [Read More]