Suppose you wanted to check the size (in bytes) of a certain type, WITHOUT using the sizeof() operator. How would you do that? And what is the size of an empty struct (or class), anyway?
Archive for November, 2009
Measuring the size of a type without sizeof
Posted in tricks on 25/11/2009 | 1 Comment »
ScopeLogger
Posted in snippets on 18/11/2009 | 13 Comments »
The RAII design pattern can be utilized to create simple and intuitive logging facilities, like the one we will present now. Through the useful macro LOG_FUNC, the proposed ScopeLogger will easily create function call graphs at run time, to allow easy debugging and tracking of program execution.
Enumerating permutations
Posted in algorithms on 14/11/2009 | 5 Comments »
There are exactly n! different permutations of n numbers. This challenge was about writing a function which is able to enumerate all these permutations, i.e. function permute(n, idx) which is able to return permutation with index idx of n numbers. The requirement is ofcourse that all these permutations must be unique – this is in [...]
Using the state design pattern to implement FSMs
Posted in design on 13/11/2009 | 1 Comment »
The State design patternĀ is a very useful design pattern. In this article we will exploit it to provide a very slick and elegant implementation of a Finite State Machine (FSM). First of all, a FSM consists of a finitie number of states and a predefined set of rulesĀ defining the transitions between all these states. Each [...]
Reset an array in constant time
Posted in algorithms on 05/11/2009 | 12 Comments »
Ever wondered how to reset an entire array of N elements in a constant slice of time? This post will introduce the algorithm along with an implementation. Let me lay out the problem. There’s an array of N integers. We would like to be able to reset that array (set all elements to zero), in [...]