Cristian Adam

NullPointerException in C++

For those familiar with languages like Java, and C#, something like NullPointerException shouldn’t come as a surprise. But what about C++? C++ also has exceptions, right?

In C++ reading or writing at address zero is an access violation. By default an access violation will result in the immediate termination of the program. What else results in immediate termination of the program? Division by zero! There is no ArithmeticException, only a swift termination!

The OS’ SDK usually provides a way to catch such access violations and recover from them. This way of catching access violations involves a C callback method and a bit of setup.

Wouldn’t be nice if the setup would be one line of code and the C callback function would throw C++ exceptions behind the scenes?

But it does work like this. At least on Windows and Linux (I don’t have access to a macOS machine), and only with a few select compilers.

Before going further into details I would like to present my test case: define functions which do:

  • Division by zero
  • Reading from nullptr
  • Writing at nullptr
  • Write to an empy vector with the subscript operator []
  • Read from an uninitialized shared_ptr

Execute them ten times to make sure that this is not only one time “wonder”. Every try block will have an instance of a RAII Message object to make sure that stack unwinding is taking place, and that we won’t have any resource leaks.

C++ I/O Benchmark

In this post I will talk about copying files. I will read one file in chunks of 1MB and write it to another file.

C++ provides three cross platform APIs for I/O (input/output):

  1. C FILE API (fopen, fread, fwrite)
  2. C++ API (std::ifstream, std::ofstream)
  3. POSIX API (open, read, write)

The POSIX API requires a bit of #ifdef-ing to get it working cross platform, but it’s not that scary.

Reading and writing 1 MB of data should work more or less as fast for all APIs, right?

I have run the benchmark on my SSD powered Lenovo Core i7 laptop running Windows 10 and Kubuntu 15.10, and on a SSD powered Raspberry PI2 running the latest Raspbian.

The code for the benchmark is below:

Introducing C++ experimental io2d

In this post I will be talking about P0267R0: A Proposal to Add 2D Graphics Rendering and Display to C++. This proposal will be discussed next week at the next ISO C++ Standardization meeting in Jacksonville.

P0267R0 comes out of C++’s SG13 HMI: Development of new proposals in selected human-machine interaction such as low-level graphics/pointing I/O primitives.

SG13 was created by Herb Sutter after the One C++ keynote talk he gave at GoingNative 2013.

Speeding up libclang on Windows

In this article I will tackle libclang’s speed on Windows, in particular Qt Creator’s clang code model.

Qt Creator 3.6.0 fixed the following bug: QTCREATORBUG-15365: Clang Model: code completion speed regression. The bug report contains information on how to enable Qt Creator’s clang code model statistics. This is done by setting this environment variable: QT_LOGGING_RULES=qtc.clangbackend.timers=true.

On Windows Qt Creator will output this information in Windows debugger output. I use DebugView to view this information.

libclang is used by Qt Creator to provide code completion support. The clang code model is still experimental and not 100% feature equivalent with the Qt Creator built-in code model.

By using the clang code model it means that Qt Creator uses a real C++ compiler to parse the source code you are editing. It also means that if you are having a big source file, with lots of includes, it will take some time to do so.

Qt Creator will cache this information in a form of a pch file under %temp%/qtc-clang-[some letters]/preamble-[some numbers].pch file. The complete compilation is done only once. The subsequent code completion commands are fast.

I have picked Lyx – The Document Processor as a test project for Qt Creator. Lyx uses Boost and Qt5 and on my Intel(R) Core (TM) i7 CPU M 620 @ 2.67 GHz Windows 10 powered laptop it takes, for Text3.cpp, approximately 10 seconds to “compile”.

Even though my laptop has multiple cores, libclang will use only one core to compile Text3.cpp. What can we do about it? It would be nice if libclang could use the GPU smile

Qt Creator 3.6.0 ships with libclang 3.6.2, and for Windows it ships a Visual C++ 2013 32 bit build, unlike Linux where 64 bit is the norm.

I will take clang 3.6.2 and compile it Visual C++ 2013, Visual C++ 2015, Clang 3.7.0 and Mingw-w64 GCC 5.3.0. I have managed to get libclang to compile Text3.cpp in approximatively 6 seconds. Which C++ compiler was able to this?

QtCreator and Google Test

In this article I will have a look on how to get started with Google Test libraries on Windows using Qt Creator for both MinGW and Visual C++.

I used the plural for Google Test libraries because there is Google Test – Google’s C++ test framework and also Google Mock – Google’s C++ mocking framework. They both are hosted on a single location on github.

Unfortunately the 2015 migration from Google Code to Github broke a lot of documentation search page links for Google Test, not to mention that the code snippets lost the syntax highlighting. disappointed

Here are the updated links for Google Test Primer and Google Mock for Dummies.

I will assume you have Qt Creator, CMake (and Ninja), MinGW and Visual C++ installed.

CMake setup

First step would be to get the master bundle zip package for both Google Mock and Google Test libraries. Then unpack the googletest-master.zip file into a directory e.g. Projects/GMock/Turtle.

Then create a CMakeLists.txt file with the following content:

cmake_minimum_required (VERSION 2.8)
project (turtle-test)

set (gtest_disable_pthreads on)

add_subdirectory (googletest-master)
config_compiler_and_linker()

add_executable (${PROJECT_NAME} mock_turtle_test.cpp)
target_link_libraries (${PROJECT_NAME} gtest gmock)

Looks simple enough. smile

Total Commander and SFTP

Having moved my blog to a static blogging engine means that now I have to upload the generated blog html files to on a server. Octopress recommends deoploying using Rsync via SSH.

Since I do my hacking on a Windows machine and I use Total Commander for file management I thought I would give Total Commander’s SFTP plugin a try.

I like to think that I am power user when it comes to Total Commander, but I ended up installing WinSCP to upload the files via SSH. I couldn’t figure out the right combination of DLL dependencies that Total Commander’s SFTP plugin requires.

Total Commander has this entry in the FAQ:

Q: Why doesn't Total Commander support a connection by SSH?

A: Unfortunately we cannot support any encryption in Total Commander because of the current patent and crypto export situation.
However, there is now a new file system plugin for Total Commander, which supports SFTP. SFTP is FTP via SSH.
It needs SSH2, which is now supported by almost all new Linux and other Unix distributions.

Since my blog is hosted in Germany, and Germany doesn’t have a crypto export situation, I thought of building the Total Commander’s SFTP plugin together with its dependencies.

Octopress on Windows

I have started using Octopress last year for my other blog (in Romanian) tastatura.info.

Octopress is advertised as “a blogging framework for hackers”. As a hacker one “should be comfortable running shell commands and familiar with the basics of Git”. But it all comes down to ruby.

If you’re a Windows hacker what do you do? My first idea was to install Cygwin.

For tastatura.info I’ve used Cygwin to run Octopress. I had a laptop with an Intel Core i7 CPU, didn’t notice any slowdowns.

By the time I’ve moved this blog to Octopress I didn’t have access to that Intel Core i7 powered laptop, but instead I had an Intel Core 2 Duo powered laptop. Then I’ve noticed that Octopress was rather slow on Cygwin.

That’s when I’ve started looking for alternatives to Cygwin.

Grim Fandango Remastered

Grim Fandango is one of my favorite adventure games and when last year it was announced that a remastered version will be available for PC, Mac, and Linux, it was too good to be true!

This year at the end of January I bought the Remastered (also available on Steam, and GOG stores) version. But as it turns out I was not able to play the game on PC!

From Blogger to Octopress

I decided to move away from Blogger blogging service to Octopress, which is “a framework designed for Jekyll, the static blogging engine powering Github Pages”.

I did the change because of two reasons:

  1. Notifications for comments. When Google introduced Google+ comments to Blogger I did the switch and for some unknown reason I am not receiving notification for comments on articles. I have spent some time trying to fix this problem without success.

  2. Syntax highlighting for code snippets. I was doing HTML exports from my text editor to include in blog posts, followed by a bit of HMTL fiddling, which is not always fun.