Posts Tagged ‘programming’

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)     { […]

Substrings in Bash

Posted: 17th October 2010 by Tim in Bash
Tags: , , , , , , , ,

There are a number of ways to extract parts of a string in bash. If you know the position of the substring you’re looking for, then you can use the ${string:offset[:length]} syntax. This works by providing a string, an offset (or starting position – remember that the first letter is in position 0) and, optionally, […]

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 […]

This tutorial is for those who have gone through the first and second LaTeX tutorials, and should give you the knowledge required to understand more specific LaTeX tips both on this site and on other websites. This tutorial will cover Packages and the basics of math mode. Packages Much like in programming, additional functionality can […]

In Bash you quite often need to check to see if a variable has been set or has a value other than an empty string. This can be done using the -n or -z string comparison operators. The -n operator checks whether the string is not null. Effectively, this will return true for every case […]

The PostgreSQL documentation states that PQexecParams can be called like so: PGresult *PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat); There are two parts of this call which can be tricky to novice C and/or PostgreSQL users and aren’t explained […]

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); […]

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 […]

Communicating through a serial port in C is pretty simple once it’s set up; you just read and write to it as though it was a file. The setting up, however, can be a real pain. Here’s the short and simple way to get it done. Note that this code requires the following headers: #include […]

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 […]