Again I had a discussion with someone from a C++ background who claimed that one should use signed integer types where possible, and who also claimed that the unsignedness of
size_t is merely a historical accident and would never be defined as such nowadays. I strongly disagree with that, so I decided to write this up, for once.
What I write here will only work with C, and can possibly extended to C++ and other languages that implement unsigned integer types, e.g good old Pascal had a
Continue reading “a praise of size_t and other unsigned types”
As we have seen in another post, functions that receive a variable argument list but which are all the same type are better replaced by a function and variadic macro. P99 has easy means to transform an argument list
x0, x2, .., xN into two parameters the first being just the length of the list, here
N+1 and an array that has the values. These types of functions can usually be much better optimized in place by modern compilers.
va_arg functions should first of all restricted to the case that the function may receive incompatible types as its argument (such as floating point and integers). If used for that the default argument promotion rules may be really harmful.
Continue reading “Avoid writing
In C, constant expressions come in two different flavors:
- integer constant expressions
- initializer constant expressions.
The naming of (1.) is (again!) sort of unfortunate, because as we will see below there are constant expressions of integer type that are not integer constant expressions in the sense of the C standard. Better think of (1.) of as compile time constant integer expressions.
Continue reading “constant expressions”
I always thought that using
NULL whenever I wanted to assign a null pointer value to a pointer was a good thing, but today I learned the contrary.
Continue reading “Don’t use
Swapping the contents of two variable is an elementary task that is often met in daily programming. There are two generic strategies to do that for general types.
Continue reading “A generic
In an earlier post we came up with a general solution to check for potential under- or overflow in an integer addition. On most modern architectures this can be done more efficiently, even when assuming that there are no special instructions that capture overflow bits or such.
Continue reading “Detecting integer overflow II”
A recent discussion on stackoverflow has shown that detecting integer overflow without provoking undefined behavior need some reflection, and that the quick answers are not necessarily the best ones.
Continue reading “Detecting integer overflow I”