Why isn’t sizeof for a struct equal to the sum of sizeof of each member?

Why does the sizeof operator return a size larger for a structure than the total sizes of the structure’s members? 12 s 12 This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS). Mis-aligned access might … Read more

What is a “cache-friendly” code?

What is the difference between “cache unfriendly code” and the “cache friendly” code? How can I make sure I write cache-efficient code? 9 s 9 Preliminaries On modern computers, only the lowest level memory structures (the registers) can move data around in single clock cycles. However, registers are very expensive and most computer cores have … Read more

C++ Singleton design pattern

Recently I’ve bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real-life example): // a lot of methods are omitted here class Singleton { public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance; }; From this declaration, I … Read more