Struct members order does make a difference

Memory usage: decreased

General modern computer address their memory in word-sized chunks, which mostly is 4-bytes word for x86 architecture or 8-bytes word for x86-64 architecture. To maintain the higher performance, computer applies rules of data alignment, which in practice means that some useless bytes can be added between variables so that the variables are accessible at addresses equal to multiple of the word size. The knowledge of that can be essential when designing structures for systems with strict memory requirements since it will allow you to reduce the size of struct.

Continue reading

Cross jumping/tail merging

Code size:   decreased or the same
Performance: worse or the same
Done by:     developer or compiler

Cross jumping or tail merging is an optimization technique used by compilers and humans. This approach is very easy to understand; first detect identical code sequences that can be shared, and then modify the program flow to work the same with the only one unique instance of this sequence.

Continue reading

Qt Exam preparations – time start

At the beginning of the year I’ve set myself a goal to pass the Qt Essentials Exam until the end of 2015. The time went by and the end of the year is coming fast — so it’s the high time to start the preparations. I’ve heard the Essentials Exam is rather easy, anyway it’s always good to be well prepared. During my study I want to collect some fundamentals into a set of questions and answers and create an unofficial web Qt Essentials quiz.

Continue reading

SQLite database with Qt – step by step

Accessing SQL databases from C++ applications is very simple with Qt library. Here is some short example that presents how to do it. I have chosen SQLite engine because it’s the easiest engine to set up (it requires no server, no configuration…), still it’s suitable for the most of possible applications.

Continue reading

Final specifier in C++11

Except of override specifier, which helps with detecting not-really-overriding methods, C++11 introduces also another specifier helpful for working with inheritance: final specifier. This one can be included to function declaration or a class definition when you want to make sure that this method will not be overriden or the class will not be used as a base class.

Continue reading

Loop unrolling

Code size:   increased
Performance: better
Done by:     developer or compiler

Loop unrolling, also known as loop unwinding, is an optimization which can reduce overhead of running a loop — number of instructions of checking the loop termination condition and loop counter modification. It requires fixed and known loop count.

For loop executes conditional statement e.g. i < 0 and modifies the loop counter e.g i++ once per iteration. It is shown in example below — a simple loop with very small body. To call this body 100 times the loop also executes conditional statement and loop counter incrementation, both 100 times. It means 200 of 300 instructions of this loop are overhead.

Continue reading

Override specifier

Introduced by: C++11

Overriding virtual functions is one of the most important mechanisms of C++ object oriented programming. The derived class method is overriding base class method when following requirements are met:

  1.  the base class method has virtual keyword,
  2. derived class method has:
    • the same name,
    • the same input parameters,
    • the same const-ness,
    • compatible return type.

If at least one of these requirement is not fulfilled, developer can end with not-overriding method and not even know about it. The C++11 standard introduces specifier override, designed for easy detection of such mistakes.

Continue reading

Private inheritance in C++

C++ standard supports three types of inheritance: public, protected and private. Usage of public inheritance is pretty common; but what about the other ones? This post will discuss private inheritance.

What’s the difference between private and public inheritance in C++? Unlike public inheritance, private inheritance is not an “is-a” relationship.

Continue reading

CppCheck – free static code analysis for C++

CppCheck is a very helpful tool for C++ programmers. It performs the static code analysis of C++ project and discovers some types of error which can be easily overlooked by developers and compilers: out of bounds or uninitialized variables, redundant code, always true/false comparisons, exception safety and many others (all checks are listed here). If you want to maintain high code quality you should include the static code checks among the development routines.

Continue reading