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 [...]
Posts Tagged ‘coding’
Namespace aliases in C++
Posted: 9th September 2010 by Tim in C++Tags: alias, C, coding, namespace, programming, style
The difference between stack and heap memory allocation
Posted: 11th August 2010 by Tim in C, C++, Software DevelopmentTags: alloc, allocation, C, coding, free, heap, malloc, memory, stack, stack overflow
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 [...]
Checking for empty string in Bash
Posted: 19th May 2010 by Tim in BashTags: Bash, coding, dash, empty, language, linux, null, programming, script, scripting, SET, shell, string, terminal
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 [...]
PQexecParams example – PostgreSQL query execution with parameters
Posted: 19th November 2009 by Tim in C, PostgreSQLTags: C, coding, exec, parameter, PostgreSQL, programming, query
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: C, code, coding, header, include, program, programming, time, timer
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: C, call, coding, development, nanosleep, programming, sleep, software engineering, usleep
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 [...]
Connecting to MySQL in C
Posted: 20th August 2009 by Tim in C, MySQL, UbuntuTags: C, coding, compile, connectivity, database, development, gcc, MySQL, sockets, sql, Ubuntu
MySQL comes with a library to make talking to MySQL with C easyish. There are a few things you have to install first, though. I’m using Ubuntu 8.04 for this walkthrough, but things should be similar for other flavours of Linux. Before we start, we have to download the development files required: sudo apt-get install [...]
Baud rate and other serial comm settings in C
Posted: 4th August 2009 by Tim in CTags: baud, C, coding, comms, communication, networking, programming, rate, serial, settings
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 [...]
Calling object functions in C++ with pthreads
Posted: 13th July 2009 by Tim in C++Tags: C, call, class, coding, function, programming, pthreads, Software Development, software engineering
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 [...]
Adding images to OpenGL in C
Posted: 7th July 2009 by Tim in OpenGLTags: bmp, C, coding, fig, image, jpeg, jpg, load, OpenGL, raw, Software Development, software engineering, texture, tif
Adding an image to a 2D OpenGL screen may seem like a relatively simple task, but it’s not. OpenGL is a rendering language, and as such has no native support for loading JPEG, GIF, TIF, BMP or any other popular image type. If, like me, you’re looking for a quick and simple way to get [...]
vbo_copy_vertices: Assertion `0′ failed.
Posted: 7th July 2009 by Tim in OpenGLTags: coding, gl, glu, glut, OpenGL, Software Development, vbo
While writing an OpenGL program, I came across the following error: vbo/vbo_exec_draw.c:135: vbo_copy_vertices: Assertion `0′ failed. The problem occured while trying to plot ~1 million points in a graph. Reducing this number to 2000 points fixed the problem. So if you’re encountering this error, try simplifying your display.