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 [...]
Archive for the ‘Software Development’ Category
bool &= bool in C++
Posted: 8th May 2012 by Tim in C++Tags: and, bitwise, bitwise-and, bool, boolean, C, logical-and, operation, operator
Highlighting tabs in Vim
Posted: 26th April 2012 by Tim in VimTags: highlight, indent, spacing, style, tab, tabs, vi, vim, vimrc
Some people like to indent code with tabs, and others like to indent with spaces. There are advantages and disadvantages to both. Regardless of which you prefer, it can be useful to see which indent style has been used. In Vim, you can do this by adding the following line to you .vimrc file (which [...]
Struct sizes in C++
Posted: 24th March 2012 by Tim in C++Tags: align, alignment, C, char, class, long, memory, size, sizeof, standard, stuct, word
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 [...]
Convert a delimited string into an array in Bash
Posted: 9th March 2012 by Tim in BashTags: array, Bash, delimiter, IFS, linux, OIFS, script, scripting, string, terminal, unix
If you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now [...]
Variable default values in Bash
Posted: 2nd October 2011 by Tim in BashTags: Bash, default, preset, sh, shell, value, var, variable
Sometimes you will be writing a script which, for example, can have some configuration changed from a command line argument. In traditional programming languages you would declare your variables with default values and then overwrite those values with the arguments which are passed in. In bash, however, you can do this all in one statement. [...]
My first FORTRAN 77 program
Posted: 27th September 2011 by Tim in FortranTags: 77, columns, compiler, first, FORTRAN, goto, label, print, program, tutorial, write
With sophisticated programming languages such as C++ and Java, it’s surprising to learn that FORTRAN is still widely used in the real world. FORTRAN 77, although approaching 35 years old, forms the backbone of many large-scale systems, and requires maintenance every now and then. I also believe that knowing basic FORTRAN 77 provides a useful [...]
Setting the maximum execution time in PHP
Posted: 5th September 2011 by Tim in PHPTags: fatal error, maximum, PHP, script, set_time_limit, time limit, web
If you’re writing a website in PHP, then by each PHP script will time out after a set time (usually 30 seconds by default). If this happen, you’ll see an error like the following: Fatal error: Maximum execution time of 30 seconds exceeded in <file> on line <line> This is a safety feature; if your [...]
Piping stderr in unix
Posted: 26th May 2011 by Tim in Bash, UbuntuTags: cerr, cout, fd, file descriptor, linux, pipe, redirect, stderr, stdout, unix
In unix, you can pass output from one program to another using the pipe symbol (|). Unfortunately, it only pipes the output from stdout (cout). You can pass the output from both stdout and stderr (cerr) by adding 2>&1 to the end of the command before the pipe, where 1 is the file descriptor for [...]
Converting a hexadecimal string to a number in C++
Posted: 14th March 2011 by Tim in C++Tags: C, dec. string, decimal, hex, hexadecimal, iostream, manipulator, oct, octal, std, stl, stream, stringstream
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 [...]
Ignoring the first line of output in a unix terminal
Posted: 31st January 2011 by Tim in Bash, Software Development, UbuntuTags: first line, ignore, linux, sed, sh, shell, terminal, Ubuntu, unix
Sometimes in a terminal you want to strip out the first line of output from a command. For example, you may want to generate a list of users which have tasks running using the ps command. This command puts a header at the top of the output. You can remove this header by piping the [...]
C/C++ Gotcha – Conditional Evaluation
Posted: 28th December 2010 by Tim in C, C++Tags: C, condition, conditional, evaluation, gotcha, if, loop, programming, while
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++ Gotcha – derived function default value
Posted: 13th December 2010 by Tim in C++Tags: argument, C, call, class, code, derived, functon, gotcha, inheritance, object, pointer, programming
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) { [...]
Bash Wildcards
Posted: 8th November 2010 by Tim in BashTags: Bash, filename, files, placeholder, shell, wild card, wildcard
There are lots crazy things you can do with bash. Some of the more useful of these are the bash wildcards. This post will explore the *, ?, {…}, [...] and [!...] wildcards. For the examples below, we will demonstrate wildcard usage with the ls command, and assume that the current directory has the following [...]
Reading from a file in C
Posted: 31st October 2010 by Tim in CTags: C, fgets, file, file handling, getline, input, program, read, string
Reading data from a file is fairly common. The stdio.h provides us with a function, getline, which allows us to read lines from a file without worrying about buffer overflows and other memory corruption issues that C is famous for. The following code opens a file named “myfile.txt” and prints out each line with the [...]
Substrings in Bash
Posted: 17th October 2010 by Tim in BashTags: Bash, manipulation, programming, script, shell, string, substr, substring, variable
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, [...]
Reading from a file in C++
Posted: 27th September 2010 by Tim in C++Tags: C, config, file, fstream, ifstream, input, read, reading, string, txt
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: alias, C, coding, namespace, programming, style
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 [...]
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 [...]
301 Redirects in PHP
Posted: 9th July 2010 by Tim in PHPTags: 301, 404, error, header, htaccess, HTML, HTTP, management, PHP, redirection, website
If you have a web page which has been moved to another URL, or if the page has been completely removed, chances are people will have that address saved in their bookmarks, or they will try to access the page via a search engine and get a 404 – Page Not Found error. Not only [...]
301 Redirects in Apache using .htaccess
Posted: 7th July 2010 by Tim in ApacheTags: 301, 404, Apache, htaccess, redirect, search, search engine, webpage
If you have a web page which has been moved to another URL, or if the page has been completely removed, chances are people will have that address saved in their bookmarks, or they will try to access the page via a search engine and get a 404 – Page Not Found error. Not only [...]
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 [...]
Pthreads are a simple and effective way of creating a multi-threaded application. This introduction to pthreads shows the basic functionality – executing two tasks in parallel and merging back into a single thread when the work has been done. First I’ll run through the basics of threading applications with pthreads. Multi-threaded applications allow two or [...]
Using Vim syntax highlighting on custom file types
Posted: 20th April 2010 by Tim in PostgreSQL, Ubuntu, VimTags: config, highlighting, linux, syntax highlighting, vim
Let’s say you have a file type whose contents are in XML format but have a different file extension such as .tim . If you want to edit these files with Vim with syntax highlighting, simply add the following to ~/.vimrc (affects only your Vim environment) or /etc/vim/vimrc (affects everyone’s Vim environment): au BufNewFile,BufRead *.tim [...]