Archive for the ‘C++’ Category

bool &= bool in C++

Posted: 8th May 2012 by Tim in C++
Tags: , , , , , , , ,

I’ve seen a few questions on the web about whether you can use the &= operator safely with bool. This operator is a bitwise-AND, so you can’t use to logically AND other datatypes. bool is just one bit, but it’s possibly stored using a whole word size. It’s not obvious what the numeric value of [...]

Struct sizes in C++

Posted: 24th March 2012 by Tim in C++
Tags: , , , , , , , , , , ,

Consider the following program: #include <iostream> struct A {     char a;     long b;     char c; }; struct B {     char a;     char c;     long b; }; int main() {     A a;     B b;     std::cout << “sizeof(a): ” << sizeof(a) << std::endl;     std::cout << “sizeof(b): ” << sizeof(b) << std::endl;     return 0; } To summarize, it [...]

In C, if you want to convert a string into a number, you can use sprintf(), atoi() or a number of other utility functions. In C++, this can be achieved much more elegantly using std::stringstream objects. But what if you’re string represents a hexadecimal number? This is where stream manipulators come into play. By passing [...]

C and C++ code is generally pretty easy to make sense of. But there are a few oddities which can catch you out and can send you into an endless debugging exercise if you’re not careful. One such oddity is conditional evaluation. Consider the following code which keeps track of three numbers. Look through the [...]

C++ can be a strange language. Most of the time it’s easy to work with, but occasionally you’ll get errors which take forever to debug. Take a look at the following code and write down what you think the output will be. #include <iostream> class Base { public:     virtual void test(int x = 0)     { [...]

Reading from a file in C++

Posted: 27th September 2010 by Tim in C++
Tags: , , , , , , , , ,

Reading data from a file is common in programming. C++ makes this process fairly painless with the ifstream class. When combined with the string class, you can avoid the memory and buffer overflow issues which you would have to deal with in C. The following example opens a file named “myfile.txt” and prints out each [...]

Namespace aliases in C++

Posted: 9th September 2010 by Tim in C++
Tags: , , , , ,

In C++, it’s good practice to explicitly specify the namespace of a class instead of using the using syntax. This makes your code more readable, more explicit and is generally just good style. Sometimes these namespaces can get unconveniently long. This is where namespace aliases come in. Imagine you have a person class in the [...]

A common question amongst coders new to C or C++ relates to the difference between stack and heap memory allocation. The answer lies in how the code is executed at the very lowest level. When a program is executed, each thread is allocated a limited amount of stack space. The stack holds information used by [...]

Occasionally you need to check whether a float is a valid number. There are times, such as when the number is read in to a function as an argument, when you can’t assume that the check has already been done. This check can be done using the isnan(number) function from math.h. For example: #include <math.h> [...]

Millisecond timer in C / C++

Posted: 18th October 2009 by Tim in C, C++
Tags: , , , , , , , ,

If you’re looking for a timer with fairly good accuracy in C or C++, you can use the functions in time.h and sys/time.h to build a millisecond timer. This is useful for things like evaluating the execution time of a program, roughly accurate to the nearest microsecond. double get_time_ms() { struct timeval t; gettimeofday(&t, NULL); [...]

The following calculations have been sourced from the Navit Project source code, released under the GNU General Public License version 2. To convert latitude and longitude into UTM X and Y coordinates, we can simply perform two calculations (lat is latitude and lon is longitude): x = lon × 6371000.0 × pi รท 180 y [...]

Nanosleep in C / C++

Posted: 29th September 2009 by Tim in C, C++
Tags: , , , , , , , ,

usleep is not a very accurate form of sleep in C / C++. From the man page: The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers. nanosleep, on the other hand, is much more accurate. The following code will [...]

Default Arguments for C++ methods

Posted: 14th July 2009 by Tim in C++

One advantage of C++ over C is the ability to have default values for arguments in a function. This is a useful feature if your method takes arguments which are mostly set to one value, or if the default value is likely to change, and very useful for constructors. The default value is specified in [...]

For various reasons, you cannot create a pthread on an object’s function. There is, however, a few ways to get around that. One of the most flexible ways of doing this is to create a wrapper function to call an object’s method, taking a pointer to the object as an argument. For example, if you [...]