ACCU – Recognising Bletchley and women in software past and present

April 30th, 2012

I’m fresh back from the ACCU conference (http://accu.org/index.php/conferences) and full of ideas to take my programming to the next level.

One thing that happened at the conference was the Speakers Dinner, and during the dinner they had an auction to raise money for Bletchley Park (http://www.bletchleypark.org.uk/).

One of the items at the auction was a small tapestry, hand woven by a 91 year old lady who used to work at the park during the war.  She had visited the park recently with her daughter and, as they like to do, the people at Bletchley Park interviewed her to find out her story.  She told them that she worked in the Japanese ciphers section decoding Japanese messages.  On hearing this her daughter said, “but mother, you don’t understand Japanese!”, to which she replied, “yes I do.”  The daughter was surprised, and asked why her mother hadn’t told her this in the past to which she replied, “You’re only 68 years old.  Why would I mention it?”

For me it’s a beautiful story that captures the modesty of how women tend to quietly get on with what needs to be done without making a big fanfare about it, which is probably one reason why we don’t hear as much about what women have done in the history of computing as we should do.

Fast forward to the auction, after a small flurry of bidding one of the women delegates put a bid in for the tapestry.  I’m sure I wasn’t the only one in the room that loved the symbolism of one generation of women in computing passing on the baton to another generation of women via something that, (perhaps in my sexist view,) was a very female artefact and couldn’t bring themselves to bid against her.

As a result I feel that the tapestry may not quite have received the amount that it could have and I would like to try and make amends for that.

I would like to invite men in computing to join me in donating to Bletchley Park.  It’s a great cause and it’s a key part of our history that enables us to do what we love today while also earning a living.  It would be such a shame for us to lose that.

And I would like to raise a few more fivers in recognition of the beautiful tapestry that this 91 year old woman created and also as recognition of what she personally contributed to our industry.

But more than that, (and this may still be the drink talking!) I would like to send a message to our female colleagues that we may sometimes say the wrong thing, we may sometimes forget that we’re not in the playground any more, we may sometimes not listen to you as long as you’d like, and sometimes we may not even look you in the eyes.  But we really do appreciate you and we really do want you in computing.  And if one day, maybe if one of us has done something really dumb, you start to question whether you should stay in the industry or should go, please, please stay.

Please donate to this unique 3 causes in 1 offer at:

http://www.justgiving.com/Bletchley-and-women-in-software

That’s the end of my lightening blog!  Thanks.

Experiments with C++11 r-value move

April 12th, 2012

One of the most fundamental additions to C++11 is r-value move semantics.

An r-value, in case you had forgotten, is, in simple terms, a variable, typically created by the compiler itself, that doesn’t have a programmer assigned name. r-values can be created as intermediate results while evaluating expressions (such as in x = a + b + c;), return values of functions, and when objects are implicitly constructed to be passed as function arguments (such as a const char * string being implicitly used to construct a std::string object that is passed to a function taking a const std::string & parameter).

I wanted to explore how these r-value moves worked, so I knocked up some code.

The Code

Here’s the code I wrote to test the r-value functionality.  It’s all here.  If you cut out my commentry you should end up with a runnable program.

Firstly I needed to include some basic headers:

#include <string>
#include <iostream>
#include <algorithm>

using std::cout;

A class for storing a name

I wanted to have a class that I could move about and I want to give that a name. It seemed simpler to include the class’s name in a separate class rather than directly in the main class, so I got:

class mover_name
{
private:
    std::string given_name;
    std::string creator_name;

public:
    mover_name() {}
    explicit mover_name( const char * p_name )
        : given_name( p_name ) {}
    mover_name( const mover_name & r_name )
        : creator_name( r_name.given_name ) {}
    mover_name( mover_name && r_name )
        : creator_name( r_name.given_name ) {}

    std::ostream & print( std::ostream & os ) const
    {
        if( ! given_name.empty() )
            os << given_name;
        else if( ! creator_name.empty() )
            os << "created by " << creator_name;
        else
            os << (void *)((int)this & 0xffff);
        return os;
    }
};

std::ostream & operator << (
                std::ostream & os,
                const mover_name & r_mover_name )
{
    return r_mover_name.print( os );
}

A class for moving about

Now I needed a class that I can move around and copy. It consists mainly of basic constructors and similar operators that print a simple message when they are invoked.  Note that I’ve added no throw specifications to the move constructors and assignment operator (even though it’s a bit of a lie in this case).  This seems to be good practice as I hope to explore in future:

class mover
{
private:
    mover_name name;

public:
    mover()
    {
        cout << "default construct [" << name << "]\n";
    }
    explicit mover( const char * p_name ) : name( p_name )
    {
        cout << "named default construct [" << name << "]\n";
    }
    mover( const mover & r_rhs ) : name( r_rhs.name )
    {
        cout << "copy construct [" << name << "]\n";
    }
    mover & operator = ( const mover & r_rhs )
    {
        cout << "[" << name << "]" <<
                " copy assigned from [" << r_rhs.name << "]\n";
        return *this;
    }
    mover( mover && r_rhs ) throw() : name( std::move( r_rhs.name ) )
    {
        cout << "move construct [" << name << "]\n";
    }
    mover & operator = ( mover && r_rhs ) throw()
    {
        cout << "[" << name << "]" <<
                " move assigned from [" << r_rhs.name << "]\n";
       return *this;
    }
    mover operator + ( const mover & r_rhs )
    {
        cout << "mover operator +\n";
        return mover( "added" );
    }
    ~mover()
    {
        cout << "destruct [" << name << "]\n";
    }

    std::ostream & print_name( std::ostream & os ) const
    {
        os << name;
        return os;
    }
};

std::ostream & operator << ( std::ostream & os, const mover & r_mover )
{
    return r_mover.print_name( os );
}

Non-r-value ref functions

Having got a class to move about, I needed some functions to move it to/from. But first it seemed reasonable to have a set of functions that did not have move semantics, and so illustrated behaviour without the new semantics:

void function_no_rref( const mover & r )
{
    cout << "In function_no_rref( const mover & r )"
                " [" << r << "]\n";
}

void function_no_rref( mover & r )
{
    cout << "In function_no_rref( mover & r )"
                " [" << r << "]\n";
}

// The following is intentionally not implemented
//void function_no_rref( mover && r )
//{
//	cout << "In function_no_rref( mover && r ) [" << r << "]\n";
//}

Functions with r-value ref params

Now we start getting to the main part of the experiment. I defined a set of functions that take different types of reference to the class defined above:

void function( const mover & r )
{
    cout << "In function( const mover & r ) [" << r << "]\n";
}

void function( mover & r )
{
    cout << "In function( mover & r ) [" << r << "]\n";
}

void function( mover && r )
{
    cout << "In function( mover && r ) [" << r << "]\n";
}

Functions with POD types

Classes are not the only thing that can be moved using C++11, so it seemed reasonable to explore how a basic POD type like int behaved:

void function( const int & r )
{
    cout << "In function( const int & r )\n";
}

void function( int & r )
{
    cout << "In function( int & r )\n";
}

void function( int && r )
{
    cout << "In function( int && r )\n";
}

Functions with string params

Simple const char * types (i.e. quoted C strings) are often implicitly converted to std::string objects in C++ so it seemed worth looking at that also:

void function( const std::string & r )
{
    cout << "In function( const std::string & r )\n";
}

void function( std::string & r )
{
    cout << "In function( std::string & r )\n";
}

void function( std::string && r )
{
    cout << "In function( std::string && r )\n";
}

Calls within functions

That covers the basic calling situation. I next wanted to explore what happens within a function that has been passed various types of reference. Hence I defined a set of functions that include a basic call to one of the functions defined above:


void function_with_call( const mover & r )
{
    cout << "In function_with_call( const mover & r )"
                " [" << r << "]\n";
    function( r );
}

void function_with_call( mover & r )
{
    cout << "In function_with_call( mover & r )"
                " [" << r << "]\n";
    function( r );
}

void function_with_call( mover && r )
{
    cout << "In function_with_call( mover && r )"
                " [" << r << "]\n";
    function( r );
}

Calls within functions using std::move

Obviously a call to a function can also use std::move, so I wanted to see how the behaviour of the previously defined functions changed if std::move was wrapped around the variable in the function call:

void function_with_call_using_move( const mover & r )
{
    cout << "In function_with_call_using_move( const mover & r )"
                " [" << r << "]\n";
    function( std::move( r ) );
}

void function_with_call_using_move( mover & r )
{
    cout << "In function_with_call_using_move( mover & r )"
                " [" << r << "]\n";
    function( std::move( r ) );
}

void function_with_call_using_move( mover && r )
{
    cout << "In function_with_call_using_move( mover && r )"
                " [" << r << "]\n";
    function( std::move( r ) );
}

That’s pretty much all the function calling conventions covered.

Functions for testing return operation

One of the main benefits of r-value moves is the ability to return values (particularly things like std::string) using a move rather than a copy, so I wanted to explore that:

mover function_return_direct()
{
    cout << "In function_return_direct()\n";
    return mover( "result" );
}

mover function_return_variable()
{
    cout << "In function_return_variable()\n";
    mover mover_return( "result" );
    return mover_return;
}

Code to run the tests

That’s all the functions I needed. Now to exercise the code. As I wanted the name of the function I was calling to be printed out followed by the result of the call I decided to cheat and define a suitable macro:

#define DO( x ) header( #x ); x

void header( const std::string & r_cmd )
{
    cout << "\n" << r_cmd << "\n";
    std::for_each( r_cmd.begin(), r_cmd.end(),
                    [](char){ cout << "-"; } );
    cout << "\n";
}

Finally to the main() function that called the code:

int main( int argc, char * argv[] )
{
    DO( mover mover_a( "A" ); );

    DO( mover mover_b( "B" ); );

    DO( mover mover_c( mover_a ); );

    DO( mover mover_d( std::move( mover_b ) ); );

    DO( mover_c = mover_b; );

    DO( mover_c = std::move( mover_a ); );

    DO( mover_c = mover_a + mover_b; );

    DO( mover_c = mover_a + mover( "temp" ); );

    DO( mover mover_e( mover( "temp2" ) ); );

    DO( function_no_rref( mover_a ); );

    DO( function_no_rref( mover( "temp func" ) ); );

    DO( function_no_rref( mover_a + mover_b ); );

    DO( function_no_rref( std::move( mover_a ) ); );

    DO( function( mover_a ); );

    DO( function( std::move( mover_a ) ); );

    DO( function( mover( "temp func" ) ); );

    DO( function( mover_a + mover_b ); );

    DO( function_with_call( mover_a ); );

    DO( function_with_call( std::move( mover_a ) ); );

    DO( function_with_call( mover( "temp func" ) ); );

    DO( function_with_call( mover_a + mover_b ); );

    DO( function_with_call_using_move( mover_a ); );

    DO( function_with_call_using_move( std::move( mover_a ) ); );

    DO( function_with_call_using_move( mover( "temp func" ) ); );

    DO( function_with_call_using_move( mover_a + mover_b ); );

    DO( mover returned( function_return_direct() ); );

    DO( mover_a = function_return_direct(); );

    DO( mover returned2( function_return_variable() ); );

    DO( mover_a = function_return_variable(); );

    mover & lref_mover = mover_a;
    DO( function_with_call( lref_mover ); );

    // mover && rref_mover = mover_a;  // Can't bind lval to r-ref
    mover && rref_mover = std::move( mover_a );
    DO( function_with_call( rref_mover ); );
    DO( function_with_call( std::move( rref_mover ) ); );

    int i = 0;
    DO( function( i ); );
    const int ci = 0;
    DO( function( ci ); );
    DO( function( 1 ); );
    DO( function( int() ); );
    DO( function( i + i ); );
    DO( function( i + ci ); );
    DO( function( i + int(ci) ); );
    DO( function( int(i + ci) ); );

    DO( function( "Hello" ); );

    cout << "\ndone\n----\n";

    return 0;
}

The results

And here’s the results.

Construction results

Firstly we have two objects to push around the place:

mover mover_a( "A" );
---------------------
named default construct [A]

mover mover_b( "B" );
---------------------
named default construct [B]

Now exploring some basic construction operations. Generally I think there are few surprises here:

mover mover_c( mover_a );
-------------------------
copy construct [created by A]

mover mover_d( std::move( mover_b ) );
--------------------------------------
move construct [created by B]

Let’s try the assignment operator:

mover_c = mover_b;
------------------
[created by A] copy assigned from [B]

mover_c = std::move( mover_a );
-------------------------------
[created by A] move assigned from [A]

Now to create a temporary object using operator +. Here the temporary is move assigned (operator = (mover &&)) to the target variable. It suggests that, similar to the rule that if you define a copy constructor you should also define a copy operator, if you define a move constructor you should also define a move operator. (Note that the results below, the line “named default construct [added]” is generated by the temporary object created as the return value of the operator + method):

mover_c = mover_a + mover_b;
----------------------------
mover operator +
named default construct [added]
[created by A] move assigned from [added]
destruct [added]

Just to check whether the behaviour changes if we force the use of a temporary object in the addition:

mover_c = mover_a + mover( "temp" );
------------------------------------
named default construct [temp]
mover operator +
named default construct [added]
[created by A] move assigned from [added]
destruct [added]
destruct [temp]

This time we create a temporary object and then use that to construct another object. You might expect that the move assignment operator is called to move the temporary object to the named object. However, in this case the compiler is smart enough to avoid creating the intermediate temporary object and creates the target object directly. Hence you should avoid having side-effects in your move operators as in some cases the compiler can avoid move operations entirely:

mover mover_e( mover( "temp2" ) );
----------------------------------
named default construct [temp2]

That’s the constructor operations explored. Now let’s explore which types of functions are called.

Calls without r-value refs

Firstly, looking at calls to a set of functions that don’t have r-value references. These should act in the same way as pre-C++11 calls:

function_no_rref( mover_a );
----------------------------
In function_no_rref( mover & r ) [A]

function_no_rref( mover( "temp func" ) );
-----------------------------------------
named default construct [temp func]
In function_no_rref( const mover & r ) [temp func]
destruct [temp func]

function_no_rref( mover_a + mover_b );
--------------------------------------
mover operator +
named default construct [added]
In function_no_rref( const mover & r ) [added]
destruct [added]

Interestingly, you can std::move to a function that does not have a r-value ref value, e.g.:

function_no_rref( std::move( mover_a ) );
-----------------------------------------
In function_no_rref( const mover & r ) [A]

Basic function calls with r-value refs

Now to introduce a set of functions that does have an r-value ref parameter.

A non-const, named variable will go to a non-const regular ref function:

function( mover_a );
--------------------
In function( mover & r ) [A]

Temporary types will go to the r-value ref variant:

function( std::move( mover_a ) );
---------------------------------
In function( mover && r ) [A]

function( mover( "temp func" ) );
---------------------------------
named default construct [temp func]
In function( mover && r ) [temp func]
destruct [temp func]

function( mover_a + mover_b );
------------------------------
mover operator +
named default construct [added]
In function( mover && r ) [added]
destruct [added]

Calls within a function

Now let’s add a function call _within_ the function we initially call and see where we end up. In the first case we end up where we expect. However, when we call a function with an r-value reference as a parameter, the function that is called inside the function is a regular reference. In other words, whether a function’s parameter is a regular reference or r-value reference doesn’t affect the type of function that is called within the function:

function_with_call( mover_a );
------------------------------
In function_with_call( mover & r ) [A]
In function( mover & r ) [A]

function_with_call( std::move( mover_a ) );
-------------------------------------------
In function_with_call( mover && r ) [A]
In function( mover & r ) [A]

function_with_call( mover( "temp func" ) );
-------------------------------------------
named default construct [temp func]
In function_with_call( mover && r ) [temp func]
In function( mover & r ) [temp func]
destruct [temp func]

function_with_call( mover_a + mover_b );
----------------------------------------
mover operator +
named default construct [added]
In function_with_call( mover && r ) [added]
In function( mover & r ) [added]
destruct [added]

Calls with std::move within a function

So let’s use std::move on the parameter when the inner function is called. In each case we get to the function with the r-value ref:

function_with_call_using_move( mover_a );
-----------------------------------------
In function_with_call_using_move( mover & r ) [A]
In function( mover && r ) [A]

function_with_call_using_move( std::move( mover_a ) );
------------------------------------------------------
In function_with_call_using_move( mover && r ) [A]
In function( mover && r ) [A]

function_with_call_using_move( mover( "temp func" ) );
------------------------------------------------------
named default construct [temp func]
In function_with_call_using_move( mover && r ) [temp func]
In function( mover && r ) [temp func]
destruct [temp func]

function_with_call_using_move( mover_a + mover_b );
---------------------------------------------------
mover operator +
named default construct [added]
In function_with_call_using_move( mover && r ) [added]
In function( mover && r ) [added]
destruct [added]

Results of function return tests

Now let’s explore returning values from a function. Note that the prototype for the function that is called is “mover function_return_direct()”, NOT “mover && function_return_direct()”. Doing the latter is just as wrong as doing “mover & function_return_direct()” (i.e. you’re passing a reference of something that may have already been destroyed).

In this first example the compiler optimizes out any copy or move operations, and constructs the return object in-situ. Once again, make sure ‘move’ does what it says on the tin and no more because it may be optimized out:

mover returned( function_return_direct() );
-------------------------------------------
In function_return_direct()
named default construct [result]

When we assign the result of a function to a pre-existing variable, then the move semantics come into play:

mover_a = function_return_direct();
-----------------------------------
In function_return_direct()
named default construct [result]
[A] move assigned from [result]
destruct [result]

We see the same pattern if the called function returns a named variable from within the function:

mover returned2( function_return_variable() );
----------------------------------------------
In function_return_variable()
named default construct [result]

mover_a = function_return_variable();
-------------------------------------
In function_return_variable()
named default construct [result]
[A] move assigned from [result]
destruct [result]

Exploring the boundaries

The above has covered the typical cases that you’ll use in code. I wanted to play around a bit more to get a more depth feel for how things work. For example, what happens if you create variables of the form “mover &” and “mover &&”?

Calling a function with a variable of type “mover &” calls the regular reference function as you’d expect:

function_with_call( lref_mover );
---------------------------------
In function_with_call( mover & r ) [A]
In function( mover & r ) [A]

However, calling a function with a “mover &&” type _also_ calls the regular reference function and not the r-value ref version. This ties in with the “function_with_call()” experiments. A named variable that is a r-value ref is treated as a regular ref for deciding which function is called:

function_with_call( rref_mover );
---------------------------------
In function_with_call( mover & r ) [A]
In function( mover & r ) [A]

You have to use std::move (or similar) to make a named r-value ref variable call an r-value ref signatured version of a function:

function_with_call( std::move( rref_mover ) );
----------------------------------------------
In function_with_call( mover && r ) [A]
In function( mover & r ) [A]

Results of PODs and r-value refs

Programmer created types are not the only ones that can make use of move semantics. Here “i” is a non-const int and “ci” is a const int:

function( i );
--------------
In function( int & r )

function( ci );
---------------
In function( const int & r )

The following create temporary, unnamed variables, hence use r-value references:

function( 1 );
--------------
In function( int && r )

function( int() );
------------------
In function( int && r )

function( i + i );
------------------
In function( int && r )

Interestingly here, even though an unnamed temporary is created, the const-ness of the ci variable seems to prevent the r-value ref version of the function being called. I wonder if this is a bug in the compiler or C++11 spec? Maybe it’s too esoteric a corner case to worry about though:

function( i + ci );
-------------------
In function( const int & r )

function( i + int(ci) );
------------------------
In function( int && r )

function( int(i + ci) );
------------------------
In function( int && r )

Results of strings calling functions

Here’s a final bonus scenario! All the results so far have been created on VS2010. If we do ‘function( “Hello” )’ on VS2010, even though an unnamed temporary std::string is created, the const-ness of the initial const char * string causes the const std::string & function to be called:

function( "Hello" );
--------------------
In function( const std::string & r )

This is how an early version of the C++11 spec was documented to work. This has been changed in the published version of C++11 and VS11 acts accordingly:

function( "Hello" );
--------------------
In function( std::string && r )

Conclusions

To conclude, an r-value ref in a function argument is only significant when deciding which of a set of overloaded functions will be called.  Once inside a function with an r-value ref the reference is treated as a regular reference.

Put another way, if a variable has a name, even if it is of type r-value ref (e.g. mover &&) it will call the regular ref version of a function, unless you use std::move. This makes sense because even if your function takes an r-value ref parameter you may want to pass the reference to a number of other functions (for example a size() function) before finally moving it on to its destination.  Not moving a value on unless you use std::move() achieves this.

Note that you can not do something like:

    mover && rref_mover = mover_a;

Instead you would need to do:

    mover && rref_mover = std::move( mover_a );

But even then calling a function with rref_mover will not call a function expecting an r-value ref.

r-value ref versions of a function will only be called if a value has no name (for example it is an intermediate result of an expression, a return value of a function, or an implicitly constructed object that is passed as a function argument) or you wrap it with std::move().

This exercise has helped me understand r-value move semantics better and I hope it’s helped you. I hope you’ll find that, while powerful, this new mechanism isn’t difficult to understand after all.

Please share:

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

Introducing the “iPhone 4S USA Edition”

April 2nd, 2012

We all know that making stuff in China is cheaper than making it in the West. But I wonder how much more would it cost to assemble an iPhone in the US rather than in China? $20? $50? $100?

It also seems clear to me that sending all your money overseas and having a long-term trade deficit is not sustainable in the long run. The companies that are doing well out of the cheap labour will eventually find that none of their domestic market will be able to afford their product.

Further, not only are the Chinese making money out of assembling products, they are inexorably climbing up the value chain so that less and less of the ‘knowledge economy’ part is done domestically. In the case of the iPhone, it may not be long before all Cupertino does is design the plastic shell, a few icons and a few story boards to describe the software flow! In other words, only the bits that make an Apple product an Apple product. All the ‘knowledge economy’ wireless chips, GPS chips and lower layer OS software will be taken over by China.

But perhaps more troubling is that, in addition to moving up the value chain for design knowledge, China has massively moved up the value-chain for manufacture knowledge. More than likely it has already overtaken the West in this area, and as China gets better, the West is actually falling down the value-chain for manufacturing. Soon it will be the case that it won’t be simply a matter of not being economical to manufacture in the West, but it will simply be impossible due to lack on know how.

In many ways, for the West, there are parallels between the decline of manufacture and global warming. Like global warming, decline in manufacturing is not critical yet, and it will take a lot of money to fix, and require a big change on the way we execute, so for the time being it’s easier just to be in denial and wait for someone else to tackle the problem.

That’s way more than I had hoped to explain the problem, and I hope that, in the interests of brevity, the problem implicitly justifies the need for a solution.

But what solution? I propose that Apple could get the ball rolling by assembling iPhones in the US and branding them as, for example, iPhone 4S USA Edition. They would be the same as regular iPhones, but include some identifiable branding, perhaps a discreet pair of red and white stripes that identified the phone as a USA Edition. The USA Edition would be more expensive, and customers could decide whether they wanted to purchase the regular edition or the USA Edition.

Why do I think this would work?

  • Apple is rich,
  • The Americans are very patriotic nation and likely to support the initiative,
  • iPhones are premium products. The list price is not the main purchasing decision criterion and many Apple customers are conscientious of the image they project,
  • It gives customers the opportunity to support their country in these tough economic times.

What’s in it for Apple?

  • It would help them deflect some of their poor Foxconn press,
  • It will contribute to them having a customer base in a few years time,
  • It would be great PR.

In summary, an iPhone 4S USA Edition seems not only a necessity, but also Win-Win for all.

But I suppose the key question is, if there was an iPhone 4S USA Edition available, would you buy one?  And how much extra would you be willing to pay?

Please share:

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

Introducing C++11 Brace-Initialization

March 29th, 2012

For an excellent article on C++11 initializers, see http://www.informit.com/articles/article.aspx?p=1852519

Bugs are coming to get you

March 28th, 2012

Be afraid.  Very afraid.  Bugs are coming to get you.  They may look innocent.  They may look like they can’t do any harm.  But they’re in your code and they’re out to get you.

Be merciless.  They are.  When you see a bug, kill it.  Kill it dead.  Better to kill it twice than half kill it and let it skulk away and join its friends.

Kill it straight away, otherwise it’ll hide.  They’re smart like that.  Otherwise you’ll forget where it was, forget what it looked like and start to wonder if it’s really there.  But it will be, waiting for when you’re guard is down and you’re most vulnerable.  And then it will strike.

Always be on your guard.  Keep looking behind you.  Look under the rocks, around the corners, because only the dumb bugs come out in the light.  Most will circle around, bide their time and then attack when you least expect it.

Don’t let them multiply.  Don’t let them collude.  Never underestimate them.  It’s you or them and they’re coming to get you.

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

Inside C++11’s shared_ptr and weak_ptr

March 27th, 2012

Now that we have C++11, we have access to shared_ptr and weak_ptr classes as part of the standard.  While classes like these have been in Boost for a long time, it seems like now is a good time to have a look inside them and see how they work.

You’re probably already familiar with what shared_ptr and weak_ptr do, but as a quick recap, a shared_ptr object points to a piece of data on the heap in such a way that you can make numerous copies of the shared_ptr object and they all share ownership of the pointed to data.  The shared ownership works such that when the last shared_ptr pointing to the data is destroyed, it also deletes the data.  This all works well until you have two objects that point to each other (or in general, any kind of loop) using shared_ptrs.  Then you end up with a circular dependency because A can’t be deleted because B is pointing to it and B can’t be deleted because A is pointing to it.  This is where weak_ptr leaps to the rescue.  If B uses a weak_ptr to point to A, then B pointing to A no longer prevents A being deleted when the final shared_ptr to A is destroyed.  If the weak_ptr in B is needed to access A, then a shared_ptr must first be created from B’s weak_ptr.  (B’s weak_ptr to A can’t be used directly to access A.)  Note that in this situation A may be destroyed before B, and so the weak_ptr in B can not always be used to create a shared_ptr to access A.

Hopefully that will be enough explanation.  What I really wanted to do here is dive in and see how this magic is implemented in code.

It turns out that the approaches taken by Boost and VS2010 are pretty similar (perhaps not surprisingly!).  I’ve summarised the high-level design in the simplified diagram below.

shared_ptr and weak_ptr structure

Essentially, when a shared_ptr is first created, it also creates an additional control block on the heap.  This contains a pointer to the data that is referenced, plus a count of the number of shared_ptrs pointing to the data (initially 1) and the number of weak_ptrs pointing to the data (in the diagram, these are called n_shared and n_weak respectively).

Each time a copy of a shared_ptr is made, or created from a weak_ptr, n_shared is incremented.  Each time a weak_ptr is made, either from a shared_ptr or another weak_ptr, n_weak is incremented.  Similarly, as the instances of shared_ptr and weak_ptr get destroyed, n_shared and n_weak are decremented accordingly.

When the last shared_ptr pointing to the data is destroyed (and n_shared goes to 0), the data is deleted.

However, there may still be instances of weak_ptr associated with the data (i.e. n_weak > 0).  Hence why it must be remembered that it is not always possible to create a shared_ptr from a weak_ptr.

Finally, when the last weak_ptr or shared_ptr associated with the data is destroyed, and both n_shared and n_weak become 0, the control block is deleted.

A few things can be observed from the way this works.  Firstly, each set of shared/weak_ptrs involves an additional control block on the heap.  Additionally, the incrementing and decrementing of the counts is often done using thread locks.

As a result, using shared_ptr and weak_ptr does not come for free.  For this reason it’s best to use std::unique_ptr wherever possible as it doesn’t involve these overheads.

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

My #OpenPatentLicensingNow Manifesto

March 15th, 2012

I’m fed up with companies abusing the patent system to gain unethical advantages over small companies.  Not only does this stifle innovation, it limits the choices available to us as consumers and means we have to pay more for our goods.

The way it works is that a large company threatens to take a small company to court over alleged violation of some bogus patent that, due to prior art or obviousness should never been granted in the first place.  Because going to court is so expensive,  it’s cheaper for the small company just to pay up, irrespective of whether the patent is valid or not.  Many have likened this to a mafia protection racket.

One heinous variant of this is to make all the licensing negotiations subject to an NDA, so that third-parties can’t see what these bully-boy companies are doing.  This means that small companies can’t collaborate on a defence, and the wisdom of crowds can’t be used to find prior art.  It’s another mafia tactic along the lines of “if you tell anyone we’re extorting you we’ll make you suffer.”

This practice of imposing NDAs must stop and it must stop now.  The patent process is a way of making innovations public to the world.  It is only reasonable that the licensing of patents should also be a public process.

To this end legislators must implement laws that require all parties involved in patent license negotiations to post copies of all correspondence on a publicly visible part of their respective company web sites.

This is a simple law to implement and no company can morally object on any sensible grounds.  If they have valid patents then they have nothing to fear from this process.  Only those who abuse the patent system have motives to fight this.

This is a tiny step on the road to patent reform, but it is an easy step, an essential step and a moral step.  We must insist on action now.

So let’s have #OpenPatentLicensingNow.

The C++ magic that allows pointers to act as iterators

March 13th, 2012

@loose_chainsaw’s blog on the C++ STL Foreach Algorithm reminded me that C++ pointers can be used in place of iterators and I was curious of the C++ magic that enabled this.

For example, we know that we can iterate over a std::vector container by doing:


    std::vector container;
    std::for_each( container.begin(), container.end(), foo );

But we can also iterate over a regular C array by doing:


    int array[20];
    std::for_each( &array[0], &array[20], foo );

So what is the C++ dark magic that enables a pointer to be used where an iterator is expected?

If we look at the code for std::for_each() in Visual Studio (in simplified form), we see that it is:


    template<class _InIt, class _Fn1>
    inline _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    {
        for (; _First != _Last; ++_First)
    	    _Func(*_First);
        return (_Func);
    }

The main operators performed on the iterator are ++, != and *.  Exactly the same operators you can perform on a pointer!  So it’s not that a pointer can act like an iterator, it’s that an iterator has the same semantics as a pointer! (In the second example the template parameter _InIt is deduced to be of type int *.)

Hence, like all magic, the C++ magic that enables a pointer to act like an iterator turns out to be just a clever bit of slight-of-hand!

Please share:

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

What matters most, icon shape, size or something else?

March 2nd, 2012

We’ve had a bonanza of releases this week, including Raspberry Pi, Windows 8 Preview and the Visual Studio 11 Beta.  I’ve registered my interest in a Raspberry Pi, and hope to get one soon.  I have already had a play with the Visual Studio 11 though.

As you are no doubt aware, Visual Studio 11 is adopting an all grey UI look and this has upset a number of developers.  VS11 is also going to trim down the set of icons on the toolbar.

When I read about the trimmed toolbars I had to admit that there were icons on the toolbar that I didn’t use, and some of them I didn’t even know what they did.  So I decided to trim down my set of icons to see whether the set I ended up with is the same as what VS11 will ship with.

What I’ve found is that my trimmed down toolbars are actually quite difficult to use, despite them being decluttered.  For example, rather than pressing the “Save” icon I’ve ended up pressing the “Paste” icon, because the latter is now where the former was!  I really did have to study the icons before I worked out that the “Paste” icon wasn’t the “Save” icon I was intending to click.

Another problem I encountered is that, because my toolbars now fit in a single row, my “Run” icon has moved from the left of the second row to the right of the first (and only) row.  I found that when I decided that I wanted to run a program my hand automatically moved over to the left hand side of the screen.  So strong was this impulse that I nearly had to use my other hand to force my mouse hand over to the right place.  It’s was as if my mouse hand was saying “You want to Run?  OK leave it to me, I know where that is.”

It seems bizarre at first, but when you think about it, that’s perfectly normal.  As we practice things like kicking balls, driving cars or developing code, the lower level motor neuron functions get pushed down into the sub-conscious levels of our brain so that we can concentrate on the higher level functions better; who to pass the ball to, how to avoid that pedestrian, and what result we expect from this test.

So that led me to my question, what matters most, icon shape, size or something else?  And I had to conclude that as an experienced software user, what mattered most was the icon position.

You could probably change the colour of the icons and to some degree the shape (as long as they were still representative of what they did), and it most likely wouldn’t affect my work that much.  But if you change the icons position I would be completely lost.

And it turns out that most of us developers have an example of this problem right in front of us in the form of the keyboard.  It’s well known that the keyboard isn’t the ideal layout for someone starting out using it.  But once you’ve learnt it, the mechanics of using it get pushed into the sub-conscious parts of the brain and it becomes automatic.  (So much so that you may find yourself repeatedly miss-spelling some words because the sub-conscious attempts to anticipate what you want to type.  For example, I recently was writing a piece of code that had the variable “ratio”, but my fingers kept writing “ration”.  Weird!  Or maybe not.  I imagine it’s because in writing “tio” is usually followed by “n” as in “caution”.)

Because we’ve all learnt how to use a keyboard and we recognise that it would be a major upheaval if we changed the layout, we don’t go changing the layout each time, say, a new version of the operating system comes out.

We should take note of this and, for the same reason, not change the layout of our icons each time we release a new version of our software.  Tweak the colours?  Yes.  Change the shape?  If you must.  But change the position?  Only as a last resort.

We should be no more inclined to change the position of our icons than we are to change the positions of the keys on a keyboard.

Please share:

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.

Possible C++11 Lambda Gotcha…

February 28th, 2012

I was experimenting with C++11 lambdas and wrote the following code:


void fiddle()
{
    int i = 10;

    auto first = [&i]()
    {
        ++i;
        std::cout << "First " << i << "\n";
    };

    auto second = [i]()
    {
        std::cout << "Second " << i << "\n";
    };

    first();
    second();
}

Basically it is setting up two named lambda expressions to act as local function definitions and then calling them.

I was expecting the result to be:


    First 11
    Second 11

However, it turned out to be:


    First 11
    Second 10

My mistake is that I was expecting the [i] capture part of the second lambda expression to be equivalent to pass-by-value when calling a function, so that “i” would be set at the time the lambda was called, not when it was declared.  However, it appears that the [i] lambda capture expression (or the  [=] capture expression in the general case) bakes the value in at the time the lambda is declared.  I believe this follows from clause 14 of the C++11 lambda specification, which reads:

14.  An entity is captured by copy if it is implicitly captured and the capture-default is =, or if  it is explicitly captured with a capture that does not include a &. For each entity captured by copy, an unnamed non-static data member is declared in the closure type. …

This could lead to some surprising and confusing results if you pre-declare lambdas.  (This shouldn’t be a problem if you declare and use a lambda in-situ, such as in a std::for_each construct.)  As a result I suggest caution when using [i] and [=] style capture specifiers for pre-declared lambdas.  Either use the [&] by reference forms, or pass the variable by value as an argument to the lambda function.

Please share:

About Codalogic

Codalogic is an ISV specialising in developing products that enhance software developer productivity. Our flagship product is a C++ XML data binding tool designed to make it easier for C++ developers to interact with XML data in their programs. It readily enables storing application configuration data in standard XML format and also reading and writing industry standard XML data.