The Difference Between Unspecified and Undefined Behaviour


What is the output of this program?

int main()
{
    int array[] = {1,2,3};
    cout << array[3] << endl;
}

Answer: Noone knows!

What is the output of this program?

void f(int i, int j){}

int foo()
{
    cout << "foo ";
    return 42;
}

int bar()
{
    cout << "bar ";
    return 42;
}

int main()
{
    f(foo(), bar());
}

Answer: Noone knows!

There is a difference in the severity of uncertainty though. The first case results in undefined behaviour (because we are indexing outside of the array), whereas the second results in unspecified behaviour (because we don’t know the order in which the function arguments will be evaluated). What is the difference?

In the case of undefined behaviour, we are screwed. Anything can happen, from what you thought should happen, to the program sending threatening letters to your neighbour’s cat. Probably it will read the memory right after where the array is stored, interpret whatever garbage is there and print it, but there is no way to know this.

In the case of unspecified behaviour however, we are probably OK. The implementation is allowed to choose from a set of well-defined behaviours. In our case, there are two possibilities, calling foo() then bar(), or bar() then foo(). Note that if foo() and bar() have some side-effects that we rely on being executed in a specific order, this unspecified behaviour would still mean we have a bug in our code.

To summarize, never write code that results in undefined behaviour, and never write code that relies on unspecified behaviour.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

I am Taking a Hiatus


I am very busy building Something™, and sadly have to downprioritize blogging for a few months. Don’t delete me from your feed reader though, there might be some sporadic posts, and I will be back in 2011! Also, I’ll keep you posted when Something™ is released.

(If you want to be notified when I resume normal operations, please add https://blog.knatten.org/feed/ to your feed reader, post a comment on this post, or send me an email at anders at knatten dot org.)

New Job, and Windows Survival 101


This Monday, I started in my new job at SPT Group. Their main product is OLGA, a multiphase flow simulator which has been around for thirty years, is the current clear market leader, and some say it laid the foundation for the entire Norwegian petroleum supplier industry.

This means I get to work in an advanced scientific/engineering domain, with some really smart guys, write new code, work out the intricacies of legacy code, and collaborate with both technical customers and partners like IFE. Plenty of challenges! And they seem to get a good score on the Joel Test, always a good sign.

The first challenge however, is surviving as a developer on Windows. Yes, I am now working full time in Visual Studio on a Microsoft Windows 7 workstation, which I am sure boggles the mind of people who know me well. Here’s a short survival guide:

  1. Get a decent computer. In my previous job, the main reason I tried to spend as little time in Windows as possible, was because I had a singe core laptop with 1 GB RAM, running Windows, antivirus, backup and all sorts of automated updates and configuration management software. Now add the development environment, and guess where it leaves you. My current workstation is an eight core Xenon with 8 GB RAM, and Windows feels almost as snappy as Ubuntu.
  2. Launchy is kind of like Gnome Do. You won’t have to navigate the start menu every time you want to launch a program, nor keep it in the taskbar, you just press Alt+Space, and start typing the first few letters of what you want to launch. In addition to launching applications, it can open documents and folders, act as a quick calculator, search the web, and more. It is also intelligent, so your most commonly used commands usually only needs one typed letter to launch.
  3. VirtuaWin is my favourite solution for multiple desktops on Windows. I have tested several, but this one is fast, open source and sufficiently advanced, with customizable keyboard shortcuts.
  4. AutoHotKey is a “free keyboard macro program. Supports hotkeys for keyboard, mouse, and joystick. Can expand abbreviations as you type them”. I currently just use it to fix the Windows window-manager, so you can alt+click anywhere in the window to resize it or move it around, without having to find the window border first. Here is the script I’m using for that. The tool is really much more versatile than this, for instance I once added “reload document” to the Foxit pdf-reader with a few lines of scripting.
  5. AstroGrep is a nice grepping tool. Grep is probably the command I miss most on Windows, and if you don’t want to go full CygWin, this is a quick and easy solution.
  6. gvim Update 6. Aug.: I forgot to mention vim, my trusty allround editor. I am developing in Visual Studio these days, but vim is great for other text editing as well.
  7. CygWin gives you the most common Linux tools, shells and commands. If you are used to doing a lot of ad hoc shell scripting, this is a must-have. I have used it quite a lot before, but I haven’t installed it here yet. When most of what I do goes on inside Visual Studio, the need hasn’t been there yet, AstroGrep has been enough. In projects where I do more command line stuff, I can’t live without it. Update 6 Aug.: Enough already, I miss bash, sed and awk. Installing CygWin now… Update 9 Aug.: If you decide to try out Cygwin, make sure you install a better terminal than the default Windows one, like mintty or rxvt.
  8. A good terminal Is there a good terminal for Windows, without running CygWin? I would appreciate some recommendations.